在上传 PHP 之前调整图像大小

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3786968/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 11:06:19  来源:igfitidea点击:

Resize image before uploading PHP

phpimageresizegd

提问by Luis

I have no idea how to resize image in PHP, my code is:

我不知道如何在 PHP 中调整图像大小,我的代码是:

for ($index = 1; $index <= 2; $index++) { 

    if (!empty($_FILES["pic$index"]["name"])) {
        $ext = substr($_FILES["pic$index"]["name"], strrpos($_FILES["pic$index"]["name"], '.') + 1);
        $dir = "../gallery/$mkdir";

        HERE I NEED THE RESIZE OF THE TMP FILE OF IMAGE

        move_uploaded_file($_FILES["pic$index"]["tmp_name"] , "$dir/img-$index.$ext");

    }  

}

$mkdir = the name of the gallery's folder (there are many galleries).

$mkdir = 画廊文件夹的名称(有很多画廊)。

$dir = where the pics will be placed.

$dir = 放置图片的位置。

$ext = the type of the image (png, gif or jpg).

$ext = 图像的类型(png、gif 或 jpg)。

foreach loop runs two times because you can upload two pics.

foreach 循环运行两次,因为您可以上传两张图片。

This script is working good, I just need to do resize and I dont have an idea how to do it..

这个脚本运行良好,我只需要调整大小,我不知道该怎么做..

回答by Lo?c Février

Here is the code I'm using to resize images.

这是我用来调整图像大小的代码。

In my case I give to the function the original file name and then the thumbnail file name.

在我的例子中,我给函数提供了原始文件名,然后是缩略图文件名。

You can adapt it for your case very easily.

你可以很容易地根据你的情况调整它。

public static function GenerateThumbnail($im_filename,$th_filename,$max_width,$max_height,$quality = 0.75)
{
// The original image must exist
if(is_file($im_filename))
{
    // Let's create the directory if needed
    $th_path = dirname($th_filename);
    if(!is_dir($th_path))
        mkdir($th_path, 0777, true);
    // If the thumb does not aleady exists
    if(!is_file($th_filename))
    {
        // Get Image size info
        list($width_orig, $height_orig, $image_type) = @getimagesize($im_filename);
        if(!$width_orig)
            return 2;
        switch($image_type)
        {
            case 1: $src_im = @imagecreatefromgif($im_filename);    break;
            case 2: $src_im = @imagecreatefromjpeg($im_filename);   break;
            case 3: $src_im = @imagecreatefrompng($im_filename);    break;
        }
        if(!$src_im)
            return 3;


        $aspect_ratio = (float) $height_orig / $width_orig;

        $thumb_height = $max_height;
        $thumb_width = round($thumb_height / $aspect_ratio);
        if($thumb_width > $max_width)
        {
            $thumb_width    = $max_width;
            $thumb_height   = round($thumb_width * $aspect_ratio);
        }

        $width = $thumb_width;
        $height = $thumb_height;

        $dst_img = @imagecreatetruecolor($width, $height);
        if(!$dst_img)
            return 4;
        $success = @imagecopyresampled($dst_img,$src_im,0,0,0,0,$width,$height,$width_orig,$height_orig);
        if(!$success)
            return 4;
        switch ($image_type) 
        {
            case 1: $success = @imagegif($dst_img,$th_filename); break;
            case 2: $success = @imagejpeg($dst_img,$th_filename,intval($quality*100));  break;
            case 3: $success = @imagepng($dst_img,$th_filename,intval($quality*9)); break;
        }
        if(!$success)
            return 4;
    }
    return 0;
}
return 1;
}

The return codes are just here to differentiate between different types of errors.

返回码只是为了区分不同类型的错误。

By looking back at that code, I don't like the "magic number" trick. I'm gonna have to change that (by exceptions for example).

回顾那段代码,我不喜欢“幻数”把戏。我将不得不改变它(例如例外)。

if (!empty($_FILES["pic$index"]["name"])) {
    $ext = substr($_FILES["pic$index"]["name"], strrpos($_FILES["pic$index"]["name"], '.') + 1);
    $dir = "../gallery/$mkdir";
    // Move it
    if(move_uploaded_file($_FILES["pic$index"]["tmp_name"] , "$dir/img-$index.$ext.tmp"))
    {
      // Resize it
      GenerateThumbnail("$dir/img-$index.$ext.tmp","$dir/img-$index.$ext",600,800,0.80);
      // Delete full size
      unlink("$dir/img-$index.$ext.tmp");
    }
} 

Use move_uploaded_file to move it (recommanded) and then you can resize it and send it to it's final destination. You might not even need the ".tmp", you can use.

使用 move_uploaded_file 移动它(推荐),然后你可以调整它的大小并将它发送到它的最终目的地。你甚至可能不需要“.tmp”,你可以使用。

    // Move it
    if(move_uploaded_file($_FILES["pic$index"]["tmp_name"] , "$dir/img-$index.$ext"))
    // Resize it
      GenerateThumbnail("$dir/img-$index.$ext","$dir/img-$index.$ext",600,800); 

回答by Aif

Keep in mind that the picture you are dealing with is alreadyuploaded on the server. You actualy want to resize picture before storing it in "safe place".

请记住,您正在处理的图片已经上传到服务器上。您实际上想在将图片存储在“安全位置”之前调整图片大小。

$_FILES["pic$index"]["tmp_name"]is probably /tmp/somepicturesname

$_FILES["pic$index"]["tmp_name"]可能是 /tmp/ somepicturesname