在服务器上调整图像大小 [英] Resize image on server
问题描述
我制作了一个负责上载图像的文件,然后将该图像移动到服务器中的文件夹中.我想我无法直接在$ _FILES数组中调整图像的大小,因此我认为必须在放入服务器后调整图像的大小,所以我的问题是,如何调整服务器中图像的大小?
I made a file that is in charge of uploading images, this images are then moved to a folder in the server. I think I can't resize the image directly in the $_FILES array so I think I must resize the image after being in the server, so my question is, how can I resize images that are in the server?
这是我拥有的代码的一部分:
This is part of the code I have:
//This is after getting target which is the file saved on the server
move_uploaded_file($_FILES[$str]['tmp_name'], $target);
scale_image($target);
现在使用scale_image()函数
Now the function scale_image()
function scale_image($image)
{
if(!empty($image)) //the image to be uploaded is a JPG I already checked this
{
$source_image = imagecreatefromjpeg($image);
$source_imagex = imagesx($source_image);
$source_imagey = imagesy($source_image);
$dest_imagex = 300;
$dest_imagey = 200;
$image = imagecreatetruecolor($dest_imagex, $dest_imagey);
imagecopyresampled($image, $source_image, 0, 0, 0, 0,
$dest_imagex, $dest_imagey, $source_imagex, $source_imagey);
}
}
但这似乎不起作用,它移动了文件但没有调整大小.
But this doesn't seems to work, it moved the file but not resized.
推荐答案
我没有在服务器目录中创建文件,所以这就是我所做的 move_uploaded_file($ _ FILES [$ str] ['tmp_name'],$ target); scale_image($ target,$ target);
I wasn't creating a file to the server's dir so this is what I did move_uploaded_file($_FILES[$str]['tmp_name'], $target); scale_image($target,$target);
现在使用scale_image()函数
Now the function scale_image()
function scale_image($image,$target)
{
if(!empty($image)) //the image to be uploaded is a JPG I already checked this
{
$source_image = imagecreatefromjpeg($image);
$source_imagex = imagesx($source_image);
$source_imagey = imagesy($source_image);
$dest_imagex = 300;
$dest_imagey = 200;
$image2 = imagecreatetruecolor($dest_imagex, $dest_imagey);
imagecopyresampled($image2, $source_image, 0, 0, 0, 0,
$dest_imagex, $dest_imagey, $source_imagex, $source_imagey);
imagejpeg($image2, $target, 100);
}
}
非常感谢大家,您给我的资源帮助我创建了此功能.
Thank you all very much, the resource you gave me helped me to create this function.
这篇关于在服务器上调整图像大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!