在PHP中裁剪图像

use*_*257 61 php gd crop

下面的代码可以很好地裁剪图像,这就是我想要的,但对于较大的图像,它也可以工作.有没有办法'缩小图像'

理想的是,我可以在裁剪之前让每张图像的大小大致相同,这样每次都能获得好的效果

代码是

<?php

$image = $_GET['src']; // the image to crop
$dest_image = 'images/cropped_whatever.jpg'; // make sure the directory is writeable

$img = imagecreatetruecolor('200','150');
$org_img = imagecreatefromjpeg($image);
$ims = getimagesize($image);
imagecopy($img,$org_img, 0, 0, 20, 20, 200, 150);
imagejpeg($img,$dest_image,90);
imagedestroy($img);
echo '<img src="'.$dest_image.'" ><p>';
Run Code Online (Sandbox Code Playgroud)

Tat*_*nen 120

如果您尝试生成缩略图,则必须先使用调整图像大小imagecopyresampled();.您必须调整图像大小,使图像较小边的大小等于拇指的相应边.

例如,如果源图像为1280x800像素且拇指为200x150像素,则必须将图像大小调整为240x150像素,然后将其裁剪为200x150像素.这使得图像的纵横比不会改变.

这是创建缩略图的通用公式:

$image = imagecreatefromjpeg($_GET['src']);
$filename = 'images/cropped_whatever.jpg';

$thumb_width = 200;
$thumb_height = 150;

$width = imagesx($image);
$height = imagesy($image);

$original_aspect = $width / $height;
$thumb_aspect = $thumb_width / $thumb_height;

if ( $original_aspect >= $thumb_aspect )
{
   // If image is wider than thumbnail (in aspect ratio sense)
   $new_height = $thumb_height;
   $new_width = $width / ($height / $thumb_height);
}
else
{
   // If the thumbnail is wider than the image
   $new_width = $thumb_width;
   $new_height = $height / ($width / $thumb_width);
}

$thumb = imagecreatetruecolor( $thumb_width, $thumb_height );

// Resize and crop
imagecopyresampled($thumb,
                   $image,
                   0 - ($new_width - $thumb_width) / 2, // Center the image horizontally
                   0 - ($new_height - $thumb_height) / 2, // Center the image vertically
                   0, 0,
                   $new_width, $new_height,
                   $width, $height);
imagejpeg($thumb, $filename, 80);
Run Code Online (Sandbox Code Playgroud)

没有测试过,但它应该工作.

编辑

现在测试和工作.

  • 只需替换`$ image = imagecreatefromjpeg($ _ GET ['src']);```$ image = imagecreatefromstring(file_get_contents($ _ GET ['src']));`. (5认同)

Era*_*nda 13

imagecopyresampled()将采取的矩形区域从$src_image宽度$src_w和高度$src_h在位置($src_x, $src_y),并将其放置在一个矩形区域$dst_image的宽度$dst_w和高度$dst_h在位置($dst_x, $dst_y).

如果源和目标坐标以及宽度和高度不同,则将执行图像片段的适当拉伸或收缩.坐标指的是左上角.

此功能可用于复制同一图像中的区域.但如果区域重叠,结果将是不可预测的.

- 编辑 -

如果$src_w$src_h是小于$dst_w$dst_h分别拇指图像将被放大,否则会被缩小.

<?php
$dst_x = 0;   // X-coordinate of destination point
$dst_y = 0;   // Y-coordinate of destination point
$src_x = 100; // Crop Start X position in original image
$src_y = 100; // Crop Srart Y position in original image
$dst_w = 160; // Thumb width
$dst_h = 120; // Thumb height
$src_w = 260; // Crop end X position in original image
$src_h = 220; // Crop end Y position in original image

// Creating an image with true colors having thumb dimensions (to merge with the original image)
$dst_image = imagecreatetruecolor($dst_w, $dst_h);
// Get original image
$src_image = imagecreatefromjpeg('images/source.jpg');
// Cropping
imagecopyresampled($dst_image, $src_image, $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h);
// Saving
imagejpeg($dst_image, 'images/crop.jpg');
?>
Run Code Online (Sandbox Code Playgroud)

  • 虽然只有代码的答案可能有效,但最好是答案至少包含一些如何工作的解释,以便人们可以理解代码而无需阅读和精神解释每行代码. (9认同)