简体   繁体   English

如何使用Gd库调整图像大小? 的PHP

[英]How to resize image using Gd library ? PHP

after receiving the image from the client , I would like to resize it and then store it . 从客户端收到图像后,我想调整大小然后存储。 I am using this function: 我正在使用此功能:

function PIPHP_ImageResize($image, $w, $h)
{
         $oldw = imagesx($image);
         $oldh = imagesy($image);
         $temp = imagecreatetruecolor($w, $h);
         imagecopyresampled($temp, $image, 0, 0, 0, 0,
            $w, $h, $oldw, $oldh);
         return $temp;
}
$image = PIPHP_ImageResize($_FILES['file']['tmp_name'],10,10);
move_uploaded_file($image, $newname);

unfortunately I got these warning: 不幸的是,我得到了这些警告:

  1. Warning: imagesx() expects parameter 1 to be resource, string given... 警告:imagesx()期望参数1为资源,给定字符串...
  2. Warning: imagesy() expects parameter 1 to be resource, string given ... 警告:imagesy()期望参数1为资源,给定字符串...
  3. Warning: imagecopyresampled() expects parameter 2 to be resource, string given ... 警告:imagecopyresampled()期望参数2为资源,给定字符串...
  4. Warning: move_uploaded_file() expects parameter 1 to be string, resource given ... 警告:move_uploaded_file()期望参数1为字符串,给定资源...

How could I fix the problem !! 我该如何解决这个问题!

imagesx expect an image resource as first parameter. imagesx期望将图像资源作为第一个参数。 You have to create one using the appropriate function, imagecreatefromjpeg or imagecreatefrompng for example. 您必须使用适当的函数(例如imagecreatefromjpegimagecreatefrompng)创建一个。

function PIPHP_ImageResize($image, $w, $h)
{
    $oldw = imagesx($image);
    $oldh = imagesy($image);
    $temp = imagecreatetruecolor($w, $h);
    imagecopyresampled($temp, $image, 0, 0, 0, 0, $w, $h, $oldw, $oldh);
    return $temp;
}

if (move_uploaded_file($_FILES['file']['tmp_name'], $newname)) {
    $uploadedImage = imagecreatefromjpeg($newname);
    if (!$uploadedImage) {
        throw new Exception('The uploaded file is corrupted (or wrong format)');
    } else {
        $resizedImage = PIPHP_ImageResize($uploadedImage,10,10);
        // save your image on disk
        if (!imagejpeg ($resizedImage, "new/filename/path")) {
              throw new Exception('failed to save resized image');
        }
    }
} else {
    throw new Exception('failed Upload');
}

I've added a minimal and insufficient error handling, you should check, for example, the format of the uploaded file and use the appropriate image creator function, or check if the uploaded file is an image. 我添加了最少且不足的错误处理,例如,您应该检查上载文件的格式并使用适当的图像创建器功能,或者检查上载的文件是否为图像。

Addendum 附录

You can determine the type of the presumed uploaded image using getimagesize function. 您可以使用getimagesize函数确定假定上传的图像的类型。

getimagesize return an array. getimagesize返回一个数组。 If the image type is supported the array[2] value return the type of the image. 如果支持图像类型,则array [2]值返回图像的类型。 if the file is not an image, or its format is not supported, the array[0] value is zero. 如果文件不是图像,或者不支持其格式,则array [0]的值为零。 Once you know the file format of the image you can use one of the imagecreatefromxxx functions to create the appropriate image. 一旦知道了图像的文件格式,就可以使用imagecreatefromxxx函数之一来创建适当的图像。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM