在PHP中裁剪图像的一部分并调整大小

问题描述:

I have a source image (can be any image with and have different dimensions). I want to be able to tell php an area from the image to crop out and resize. For example:

  • The source image is 800x800 pixels.
  • PHP selects a 50x50 pixel region of the image with the selection area starting 80 pixels from the top edge of the source image, and 100 pixels from the left edge of the source image
  • The selected region is then resized to 37x37 pixels.

I tried using the functions imagecopyresampled() and imagecopyresized() but they both gave me a weird problem: imagecopyresampled() outputs the selection image but is surrounded by a black area equivalent to the dimensions of the source image. imagecopyresized() also selects the selected region, but it is just overlayed on the source image. The end goal here is to make thumbnails from the source image.

An example of the code I am already using is:

list($width_orig, $height_orig) = getimagesize($filename);
imagecopyresized($image_final, $image, 0, 0, 0, 0, 37, 37, $width_orig, $height_orig);

The above does not do exactly as the task describes, but it demonstrates the problem that I am running into.

Note:

I understand that doing a search on this topic leads to a lot of results but they mostly reference libraries and classes that do a lot more than I actually need. I'd rather have an understanding of the solution and implement something more efficient. Hence, I am not looking to be pointed to an exhaustive script that accomplishes the above task and 50 other tasks. I'd rather understand the above so I can create an optimal, custom solution.

我有一个源图像(可以是任何具有不同尺寸的图像)。 我希望能够从图像中告诉php一个区域来裁剪和调整大小。 例如: p>

  • 源图像为800x800像素。 li>
  • PHP选择图像的50x50像素区域,选择区域为80 来自源图像上边缘的像素和源图像左边缘的100个像素 li>
  • 然后将所选区域的大小调整为37x37像素。 li> ul>

    我尝试使用函数imagecopyresampled()和imagecopyresized()但它们都给了我一个奇怪的问题:imagecopyresampled()输出选择图像但是被一个等同于源图像尺寸的黑色区域包围 。 imagecopyresized()也会选择所选区域,但它只是覆盖在源图像上。 这里的最终目标是从源图像制作缩略图。 p>

    我已经使用的代码示例是: p>

      list  ($ width_orig,$ height_orig)= getimagesize($ filename); 
    imagecopyresized($ image_final,$ image,0,0,0,37,37,$ width_orig,$ height_orig); 
      code>   pre> 
     
     

    上述内容并不完全与任务描述相同,但它表明了我遇到的问题。 p>

    注意: strong > p>

    我知道对这个主题进行搜索会产生很多结果,但它们主要引用的库和类比我实际需要的要多得多。 我宁愿了解解决方案并实现更有效的方法。 因此,我不希望被指向完成上述任务和其他50项任务的详尽脚本。 我宁愿理解 em>以上内容,这样我就可以创建一个最佳的自定义解决方案。 p> div>

Try my image manipulator class, it does exactly what you want - http://github.com/philBrown/Tolerable/blob/master/library/Tolerable/Image/Manipulator.php

Your question is in fact a logic question. Can you check that snipped? Scales if it does not fit crops out longer part.

    $w  = imagesx($img);
    $h  = imagesy($img);
    $newImage = imagecreatetruecolor($wNewOrj, $hNewOrj);
    if(($w/$h)<($wNewOrj/$hNewOrj)){
      $hNew = $h * $wNewOrj/$w;
      $wNew = $wNewOrj;
    }else{
      $hNew = $hNewOrj;
      $wNew = $w * $hNewOrj/$h;
    }
    $wDiff  = round(abs($wNewOrj-$wNew)/2);
    $hDiff  = round(abs($hNewOrj-$hNew)/2);
    imagecopyresampled($newImage, $img,
                       0,     0,     $wDiff,    $hDiff,
                       $wNew, $hNew, $w-$wDiff, $h-$hDiff);

    imagejpeg($newImage, $path, 85);