【问题标题】:Resize image before uploading PHP在上传 PHP 之前调整图像大小
【发布时间】:2011-04-16 18:19:05
【问题描述】:

我不知道如何在 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 = 画廊文件夹的名称(有很多画廊)。

$dir = 放置图片的位置。

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

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

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

【问题讨论】:

  • 请注意,您不能在上传之前调整图像的大小 - 它需要上传以便 php 可以调整它的大小。
  • 你确定吗?所以我必须上传它并调整它的大小,然后再次上传?
  • 嗨 Luis,请问...为什么不允许上传完整的图片大小并在页面呈现时调整图片大小?
  • 嘿,因为600*600的图片尺寸会比200*200大..
  • move_uploaded_file() 不要上传任何东西。它只是将文件从一个目录移动到另一个在同一服务器上,上传之后。

标签: php image resize gd


【解决方案1】:

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

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

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

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;
}

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

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

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");
    }
} 

使用 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); 

【讨论】:

  • th_filename 和 im_filename 是什么?另外,我可以删除“// Let's create the directory if needed..”的部分吗?。
  • 是图片文件名和缩略图文件名。 “拍摄该图像,制作缩略图并将其保存在那里”。如果您确定它已经存在,您可以删除该目录的部分。
  • 你确定这个功能没问题?你不能写: if(!$src_im) return 3;例如。
  • 好吧,如果你说我相信你。 :-) 但我真的不明白这个功能.. 返回数字(0,1,2,3,4) - 为什么?以及我在代码中的哪个位置?
  • @Luis : 我修改了函数来测试 move_uploaded_file 的返回值
【解决方案2】:

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

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

【讨论】:

    猜你喜欢
    • 2015-12-05
    • 2012-09-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-22
    • 1970-01-01
    相关资源
    最近更新 更多