在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>'; 

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

例如,如果您的源图像是1280×800像素,而您的拇指是200×150像素,则必须将图像大小调整为240×150像素,然后将其裁剪为200×150像素。 这是因为图像的高宽比不会改变。

以下是创build缩略图的一般公式:

 $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); 

没有testing过,但它应该工作。

编辑

现在testing和工作。

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

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

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

– 编辑 –

如果$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'); ?> 
 $image = imagecreatefromjpeg($_GET['src']); 

需要被replace为:

 $image = imagecreatefromjpeg('images/thumbnails/myimage.jpg'); 

因为imagecreatefromjpeg()期待一个string。
这对我有效。

参考:
http://php.net/manual/en/function.imagecreatefromjpeg.php

PHP 5.5有一个imagecrop函数http://php.net/manual/en/function.imagecrop.php

有没有“缩小图像”的方法。

对于较less的服务器端/ PHP方法,这里有一个不错的jQuery插件 。

人们可以在客户端进行所有必要的调整 – 缩放和纵横比种类 – 并将最终裁剪的区域位置和大小发送到服务器端以进行最终的操纵和保存。 文件说,你已经足够摇晃。

HTML代码: –

 enter code here <!DOCTYPE html> <html> <body> <form action="upload.php" method="post" enctype="multipart/form-data"> Select image to upload: <input type="file" name="image" id="fileToUpload"> <input type="submit" value="Upload Image" name="submit"> </form> </body> </html> 

upload.php的

 enter code here <?php $image = $_FILES; $NewImageName = rand(4,10000)."-". $image['image']['name']; $destination = realpath('..http://img.dovov.comtesting').'/'; move_uploaded_file($image['image']['tmp_name'], $destination.$NewImageName); $image = imagecreatefromjpeg($destination.$NewImageName); $filename = $destination.$NewImageName; $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); echo "cropped"; die; ?> 
 $image = imagecreatefromjpeg($_GET['src']); $filename = 'images/cropped_whatever.jpg' 

必须replace为:

 $image = imagecreatefromjpeg($_GET['src']); 

那么它会工作!