在 PHP 中裁剪图像
下面的代码可以很好地裁剪图像,这正是我想要的,但对于较大的图像,它也不起作用。有没有什么方法可以“缩小图像”
理想情况下,我可以在裁剪之前使每个图像的大小大致相同,这样每次
代码都可以得到很好的结果
<?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>';
The code below crops the image well, which is what i want, but for larger images, it wotn work as well. Is there any way of 'zooming out of the image'
Idealy i would be able to have each image roughly the same size before cropping so that i would get good results each time
Code is
<?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>';
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
如果您尝试生成缩略图,则必须首先使用
imagecopyresampled();
调整图像大小。您必须调整图像大小,使图像较小一侧的大小等于拇指相应一侧的大小。例如,如果您的源图像为 1280x800 像素,而您的拇指为 200x150 像素,则必须将图像大小调整为 240x150 像素,然后将其裁剪为 200x150 像素。这样图像的纵横比就不会改变。
以下是创建缩略图的一般公式:
尚未对此进行测试,但它应该可以工作。
编辑
现已测试并正常工作。
If you are trying to generate thumbnails, you must first resize the image using
imagecopyresampled();
. You must resize the image so that the size of the smaller side of the image is equal to the corresponding side of the thumb.For example, if your source image is 1280x800px and your thumb is 200x150px, you must resize your image to 240x150px and then crop it to 200x150px. This is so that the aspect ratio of the image won't change.
Here's a general formula for creating thumbnails:
Haven't tested this but it should work.
EDIT
Now tested and working.
imagecopyresampled()
将从$src_image
中获取宽度$src_w
和高度$src_h
的矩形区域,位置 < code>($src_x, $src_y) 并将其放置在宽度$dst_w
和高度$dst_h
的$dst_image
矩形区域中code> 位于($dst_x, $dst_y)
位置。如果源坐标和目标坐标以及宽度和高度不同,则会对图像片段进行适当的拉伸或收缩。坐标指的是左上角。
此功能可用于复制同一图像内的区域。但如果区域重叠,结果将难以预测。
- 编辑 -
如果
$src_w
和$src_h
分别小于$dst_w
和$dst_h
,缩略图将放大。否则将缩小。imagecopyresampled()
will take a rectangular area from$src_image
of width$src_w
and height$src_h
at position($src_x, $src_y)
and place it in a rectangular area of$dst_image
of width$dst_w
and height$dst_h
at position($dst_x, $dst_y)
.If the source and destination coordinates and width and heights differ, appropriate stretching or shrinking of the image fragment will be performed. The coordinates refer to the upper left corner.
This function can be used to copy regions within the same image. But if the regions overlap, the results will be unpredictable.
- Edit -
If
$src_w
and$src_h
are smaller than$dst_w
and$dst_h
respectively, thumb image will be zoomed in. Otherwise it will be zoomed out.php 5.5 有一个 imagecrop 函数 http://php.net/manual/en/function.imagecrop .php
php 5.5 has an imagecrop function http://php.net/manual/en/function.imagecrop.php
需要替换为:
因为
imagecreatefromjpeg()
需要一个字符串。这对我有用。
参考:
http://php.net/manual/en/function.imagecreatefromjpeg.php
Needs to be replaced with this:
Because
imagecreatefromjpeg()
is expecting a string.This worked for me.
ref:
http://php.net/manual/en/function.imagecreatefromjpeg.php
HTML 代码:-
upload.php
HTML Code:-
upload.php
改进了 PHP 中的动态裁剪图像功能。
cropimage.php
中的代码Improved Crop image functionality in PHP on the fly.
Code in
cropimage.php
您可以在 ( PHP 5 >= 5.5.0,PHP 7)
示例:
You can use
imagecrop
function in (PHP 5 >= 5.5.0, PHP 7)Example:
必须替换为:
然后就可以了!
Must be replaced with:
Then it will work!