PHP:使用X,Y坐标创建缩略图

PHP:使用X,Y坐标创建缩略图,php,sql,image,resize,thumbnails,Php,Sql,Image,Resize,Thumbnails,如何获取已上载到服务器的500x500(或任意大小)图像,并根据定义的特定x、y坐标生成新图像?例如(0,0)到(50,0);(0,50)至(50,50)。我不想将图像的大小缩小到50x50px,而是想抓住图像的左上部分,并在某种意义上“裁剪”它以用作缩略图 如何在PHP中执行此操作? 你想用。首先创建具有所需尺寸的图像,然后使用imagecopy将源图像的一部分复制到新图像中: // use whatever mechanism you prefer to load your source

如何获取已上载到服务器的500x500(或任意大小)图像,并根据定义的特定x、y坐标生成新图像?例如(0,0)到(50,0);(0,50)至(50,50)。我不想将图像的大小缩小到50x50px,而是想抓住图像的左上部分,并在某种意义上“裁剪”它以用作缩略图

如何在PHP中执行此操作?

你想用。首先创建具有所需尺寸的图像,然后使用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);
你想用。首先创建具有所需尺寸的图像,然后使用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);

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

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函数中设置质量参数

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


我已经找到了一些方法。如果您想动态生成拇指,可以使用:

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函数中设置质量参数

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


您是否希望脚本生成缩略图并将其保存到文件夹中以供以后使用?还是希望它动态生成?我想将它们保存到一个文件夹中供以后使用。请看一下,您是否希望脚本生成缩略图并将它们保存到一个文件夹中供以后使用?或者你想让它在运行中生成?我想把它们保存到一个文件夹中供以后使用。请看