我试图以与裁剪工具相同的方式保存图像,使用复制我得到类似X = 443
和Y = 180
以及Width = 180
和height = 180
的内容,现在我的问题是如何将其发送到php以调整原始图像的大小,因为图像编辑器使用上述信息显示。目前我使用下面的PHP代码调整图像和高度,但添加X和Y是一直给我的问题。
<?php
function CroppedThumbnail($imagename, $path, $width, $height, $rename, $imgQuality, $ratio=false){
$url = '';
$info = pathinfo($imagename);
$newpath = $path;
$cdn_newpath = 'cdn/';
//Rename the image
if($rename == true){ $newFileName = $newpath . '/' . substr($info['filename'],0,5) . '-' . $width . 'x' . $height . '.jpeg';}
else{ $newFileName = $newpath . '/' . $imagename;}
$imageAvatar = substr($info['filename'],0,5) . '-' . $width . 'x' . $height . '.jpeg';
if(!file_exists($cdn_newpath.$newpath)){
mkdir($cdn_newpath.$newpath, 0777, true);
chmod($cdn_newpath.$newpath, 0755);
}
if(is_dir($cdn_newpath.$newpath . '/') && file_exists($cdn_newpath.$newFileName)){ return $url . $newFileName;}
else{
// Resample
if ($info['extension'] == 'jpeg' OR $info['extension'] == 'jpg'){ $image = imagecreatefromjpeg($imagename);}
else if ($info['extension'] == 'gif'){ $image = imagecreatefromgif($imagename);}
else if ($info['extension'] == 'png'){ $image = imagecreatefrompng($imagename);}
$widthx = imagesx( $image );
$heighty = imagesy( $image );
// calculate thumbnail size
if($ratio == true){
$new_width = $width;
$new_height = floor( $heighty * ( $width / $widthx ) );
$image_p = imagecreatetruecolor( $new_width, $new_height );
imagecopyresampled( $image_p, $image, 0, 0, 0, 0, $new_width, $new_height, $widthx, $heighty );
}else{
$image_p = imagecreatetruecolor($width, $height);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $width, $height, $widthx, $heighty);
}
imagejpeg($image_p, $cdn_newpath.$newFileName);
return $url . $newFileName;
}
}
?>
答案 0 :(得分:0)
你面临同样的问题。请参阅下面从我的课程中提取的代码段。有些代码会看起来像伪代码。
$trueFactorx = $original_height/$current_height;
$trueFactory = $original_width/$current_width;
// calls to my wrapper functions
$img=image_crop($img,$crop_cordinates,$trueFactorx,$trueFactory);
save_image($img,$image_type,$new_image_path);
function image_crop($img1,$crop_cordinates,$factorX,$factorY)
{
$corrdinate_array=explode(",", $crop_cordinates);
$x=$corrdinate_array[0] * $factorX;
$y=$corrdinate_array[1]* $factorY;
$height=$corrdinate_array[2]* $factorY;
$width=$corrdinate_array[3]* $factorX;
$img2 = imagecreatetruecolor($width, $height);
imagecopy($img2, $img1, 0, 0, $x, $y, $width, $height);
return $img2;
}
function save_image($img,$image_type,$new_image_path)
{
switch(strtolower($image_type))
{
case 'png':
header( 'Content-Type: image/png' );
imagepng($img,$new_image_path);
break;
case 'gif':
imagegif($img,$new_image_path);
break;
case 'jpeg':
imagejpeg($img,$new_image_path);
break;
case 'jpg':
imagejpeg($img,$new_image_path);
break;
default:
die('Unsupported File!');
}
imagedestroy($img);
}
祝你好运!