【问题标题】:Upload image, create its thumb then save上传图片,创建拇指然后保存
【发布时间】:2011-03-18 03:06:42
【问题描述】:

假设我上传了一张图片。我可以获取它的临时目录,然后用move_uploaded_file() 保存它,但是如果我还想创建一个拇指并将两者都保存在某个文件夹中怎么办?

我知道如何保存上传的图片,但我不知道如何开始操作图片并在创建拇指后保存。

【问题讨论】:

  • 你不需要在标题中加上“[PHP]”,这就是标签的用途。

标签: php image image-manipulation thumbnails


【解决方案1】:

您需要php gdimagemagick。这是一个使用 gd 调整大小的快速示例(来自手册):

<?php
// File and new size
$filename = 'test.jpg';
$percent = 0.5;

// Content type
header('Content-type: image/jpeg');

// Get new sizes
list($width, $height) = getimagesize($filename);
$newwidth = $width * $percent;
$newheight = $height * $percent;

// Load
$thumb = imagecreatetruecolor($newwidth, $newheight);
$source = imagecreatefromjpeg($filename);

// Resize
imagecopyresized($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);

// Output
imagejpeg($thumb, 'thumbs/thumb1.jpg');
?>

【讨论】:

  • 如何将图像保存在某个文件夹中而不是显示它?
  • imagejpeg函数的第二个参数是文件名。我已经更新了这个例子。另外,请查看手册页:php.net/manual/en/function.imagejpeg.php
【解决方案2】:

GD 库使这变得超级简单。

您需要计算原始图像的尺寸以保持宽高比,然后将图像重新采样为较小的尺寸。这是一个很好的教程,清楚地解释了一切:http://www.phptoys.com/e107_plugins/content/content.php?content.46

【讨论】:

    【解决方案3】:

    使用图像魔法。 检查堆栈溢出的先前帖子 Imagemagick thumbnail generation with php - using -crop PHP: Creating cropped thumbnails of images, problems Image processing class in PHP http://www.imagemagick.org/

    define('THUMB_WIDTH', 60);
    define('THUMB_HEIGHT', 80);
    define('MAGICK_PATH','/usr/local/bin/');
    
    function makeThumbnail($in, $out) {
        $width = THUMB_WIDTH;
        $height = THUMB_HEIGHT;
        list($w,$h) = getimagesize($in);
    
        $thumbRatio = $width/$height;
        $inRatio = $w/$h;
        $isLandscape = $inRatio > $thumbRatio;
    
        $size = ($isLandscape ? '1000x'.$height : $width.'x1000');
        $xoff = ($isLandscape ? floor((($inRatio*$height)-$width)/2) : 0);
        $command = MAGICK_PATH."convert $in -resize $size -crop {$width}x{$height}+{$xoff}+0 ".
            "-colorspace RGB -strip -quality 90 $out";
    
        exec($command);
    }
    

    【讨论】:

      【解决方案4】:

      我总是使用 Verot PHP Upload 类并且总是成功。这个 PHP 类实现起来非常简单,并且可以以任何您想要的方式操作图像。它也可以将图像保存在指定的文件夹中。

      您可以从here下载它

      要查看 Upload 类的演示和简单文档,请访问 http://www.verot.net/php_class_upload_samples.htm?PHPSESSID=5375147e959625e56e0127f3458a6385

      下面是我从网站上得到的一个简单示例

      //How to use it?
      //Create a simple HTML file, with a form such as:
      
       <form enctype="multipart/form-data" method="post" action="upload.php">
         <input type="file" size="32" name="image_field" value="">
         <input type="submit" name="Submit" value="upload">
       </form>
      
      //Create a file called upload.php:
      
        $handle = new upload($_FILES['image_field']);
        if ($handle->uploaded) {
            $handle->file_new_name_body   = 'image_resized';
            $handle->image_resize         = true;
            $handle->image_x              = 100;
            $handle->image_ratio_y        = true;
            $handle->process('/home/user/files/');
            if ($handle->processed) {
                echo 'image resized';
                $handle->clean();
            } else {
                echo 'error : ' . $handle->error;
            }
        }
      
      //How to process local files?
      //Use the class as following, the rest being the same as above:
      
        $handle = new upload('/home/user/myfile.jpg');
      

      【讨论】:

        猜你喜欢
        • 2014-12-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-11-20
        • 1970-01-01
        • 1970-01-01
        • 2012-03-23
        • 1970-01-01
        相关资源
        最近更新 更多