【问题标题】:PHP: Create Thumbnail Image Using X,Y CoordinatesPHP:使用 X、Y 坐标创建缩略图图像
【发布时间】:2011-12-31 18:05:40
【问题描述】:

如何获取已上传到服务器的 500x500(或任何尺寸)图像并从定义的特定 x,y 坐标生成新图像?例如(0,0)到(50,0); (0,50) 到 (50,50)。我不想将图像的大小调整为 50x50 像素,而是想抓取图像的左上部分,并在某种意义上“裁剪”它以用作缩略图。

我怎样才能在 PHP 中做到这一点?

【问题讨论】:

  • 您是否希望脚本生成缩略图并将其保存到文件夹以供以后使用?还是您希望它即时生成?
  • 我想将它们保存到一个文件夹中以备后用。

标签: php sql image resize thumbnails


【解决方案1】:

您想使用imagecopy。首先创建一个具有您想要的尺寸的图像,然后使用 imagecopy 将源图像的一部分复制到新图像中:

// use whatever mechanism you prefer to load your source image into $image
$width = 50;
$height = 50;
// Define your starting coordinates in the source image.
$source_x = 20;
$source_y = 100;
$new_image = imagecreatetruecolor($width, $height);
imagecopy($new_image, $image, 0, 0, $source_x, $source_y, $width, $height);
// Now $new_image has the portion cropped from the source and you can output or save it.
imagejpeg($new_image);

【讨论】:

    【解决方案2】:

    http://www.php.net/manual/en/function.imagick-cropthumbnailimage.php#81547

    $image = new Imagick($path."test1.jpg");
    
    $image->cropThumbnailImage(160,120); // Crop image and thumb
    
    $image->writeImage($path."test1.jpg");
    

    【讨论】:

      【解决方案3】:

      我已经看到了几种方法来做到这一点。如果您想动态生成拇指,您可以使用:

      function make_thumb($src,$dest,$desired_width)
      {
      
        /* read the source image */
        $source_image = imagecreatefromjpeg($src);
        $width = imagesx($source_image);
        $height = imagesy($source_image);
      
        /* find the "desired height" of this thumbnail, relative to the desired width  */
        $desired_height = floor($height*($desired_width/$width));
      
        /* create a new, "virtual" image */
        $virtual_image = imagecreatetruecolor($desired_width,$desired_height);
      
        /* copy source image at a resized size */
        imagecopyresized($virtual_image,$source_image,0,0,0,0,$desired_width,$desired_height,$width,$height);
      
        /* create the physical thumbnail image to its destination */
        imagejpeg($virtual_image,$dest);
      }
      

      也可以在imagejpeg函数中设置质量参数。

      或者,如果您想将图像缩略图保存到我会查看的目录中:

      http://bgallz.org/270/php-create-thumbnail-images/

      http://www.imagemagick.org/script/index.php

      【讨论】:

        猜你喜欢
        • 2022-01-13
        • 2012-11-26
        • 1970-01-01
        • 2011-02-09
        • 2021-07-10
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-10-27
        相关资源
        最近更新 更多