<?php
$targ_w = $_POST['targ_w'];
$targ_h = $_POST['targ_h'];
$jpeg_quality = 90;
$src = $_POST['photo_url'];
// create new jpeg image based on the target sizes
$img_r = imagecreatefromjpeg($src);
$dst_r = ImageCreateTrueColor( $targ_w, $targ_h );
imagecopyresampled($dst_r,$img_r,0,0,$_POST['x'],$_POST['y'], $targ_w,$targ_h,$_POST['w'],$_POST['h']);
imagejpeg($dst_r,$src,$jpeg_quality);
// display the photo - "?time()" to force refresh by the browser
echo '<img src="'.$src.'?'.time().'">';
exit;
?>
我正在编写用于图像裁剪的代码,并使用上述代码保存了裁剪后的图像。我可以使用imagecreatefromjpeg
裁剪和保存jpg文件,也可以将功能更改为imagecreatefrompng
。我可以裁剪png文件,请一次使用此代码帮助我验证或保存多种类型的图像
谢谢!
答案 0 :(得分:0)
$src = $_POST['photo_url'];
$what = getimagesize($src);
switch(strtolower($what['mime']))
{
case 'image/png':
$img_r = imagecreatefrompng($src);
$source_image = imagecreatefrompng($src);
$type = '.png';
break;
case 'image/jpeg':
$img_r = imagecreatefromjpeg($src);
$source_image = imagecreatefromjpeg($src);
error_log("jpg");
$type = '.jpeg';
break;
case 'image/gif':
$img_r = imagecreatefromgif($src);
$source_image = imagecreatefromgif($src);
$type = '.gif';
break;
default: die('image type not supported');
}
谢谢您,这是有效的任何类型图像的必需代码