php 裁切问题,求大神指点指点,弄了一天了...
我前端用jcrop获取到x、y坐标、宽度、高度,传递到 uphoto.php处理,现在一切正常,但是如果前端用户选择的图片宽度过大(我用css样式控制用户选择的图片最大宽度为680px),裁切出来的就不正常了...我查阅了一些资料,说是需要按比例换算,但是是按什么比例换算啊,用户选择的图片的宽度(大于680px时)和680换算么?
这是 uphoto.php:
$x1 = $_post[x1];
$x2 = $_post[x2];
$y1 = $_post[y1];
$y2 = $_post[y2];
$targ_w = $_post[w];
$targ_h = $_post[h];
if ($_files[mfile][error]){echo error: . $_files[file][error];}
if (!empty($_files[mfile][name])){ //提取文件域内容名称,并判断
$path = /upload/user/photo/; //上传路径
//检查是否有该文件夹,如果没有就创建,并给予最高权限
//if(!file_exists($path)){mkdir($path, 0700);}
//允许上传的文件格式
$allow_type = array(image/gif,image/pjpeg,image/jpeg,image/png);
//检查上传文件是否在允许上传的类型
if(!in_array($_files[mfile][type], $allow_type)){echo error:格式有误,仅支持gif/jpg/png; exit();}
//检测图片大小
if($_files[mfile][size] > 5*1024*1024){echo error:文件不得超过5m; exit();}
$filetype = $_files[mfile][type];
if($filetype == image/jpeg){
$img_type = .jpg;
}
if ($filetype == image/jpg) {
$img_type = .jpg;
}
if ($filetype == image/pjpeg) {
$img_type = .jpg;
}
if($filetype == image/gif){
$img_type = .gif;
}
if($filetype == image/png){
$img_type = .png;
}
if($_files[mfile][name])
{
$randstr = random_str().date(ymdhis); //获取时间并赋值给变量
$saveto = $_server['document_root'].$path.$randstr.$img_type; //图片的完整路径
$newimg = $randstr.$img_type; //图片名称
$flag=1;
}
//end if
if ($flag){$result = move_uploaded_file($_files[mfile][tmp_name], $saveto);}
//特别注意这里传递给move_uploaded_file的第一个参数为上传到服务器上的临时文件
$src_file = $saveto;
}}
$type = exif_imagetype($src_file);
$support_type = array(imagetype_jpeg , imagetype_png , imagetype_gif);
if(!in_array($type, $support_type, true)){echo error:当前图片格式非jpg/png/gif;exit();}
//load image
switch($type) {
case imagetype_jpeg :
$src_img = imagecreatefromjpeg($src_file);
break;
case imagetype_png :
$src_img = imagecreatefrompng($src_file);
break;
case imagetype_gif :
$src_img = imagecreatefromgif($src_file);
break;
default:
echo load image error!;
exit();
}
$dst_r = imagecreatetruecolor($targ_w, $targ_h);
list($width_orig, $height_orig) = getimagesize($src_file);
//if ($width_orig > 680){
//$new_width_orig = 680;
//$new_height_orig = ($height_orig*680)/$width_orig;
//}
//else {
//$new_width_orig = $targ_w;
//$new_height_orig = $targ_h;
//}
//echo $new_width_orig.
;
//echo $new_height_orig.
;
//echo x:.$x1.
;
//echo y:.$y1;
//exit();
imagecopyresampled($dst_r, $src_img, 0, 0, $x1, $y1, $targ_w, $targ_h, $targ_w, $targ_h);
//imagecopyresampled($dst_r, $src_img, 0, 0, $x1, $y1, $targ_w, $targ_h, $targ_w, $targ_h);
switch($type) {
case imagetype_jpeg :
header('content-type: image/jpeg');
imagejpeg($dst_r, null, 100);
break;
case imagetype_png :
header('content-type: image/png');
imagepng($dst_r, null, 100);
break;
case imagetype_gif :
header('content-type: image/gif');
imagegif($dst_r, null, 100);
break;
default:
break;
}
echo ;
exit;
求大神指点指点
------解决思路----------------------
用css样式控制用户选择的图片最大宽度为680px
就表示用户看到的是经过动态缩放的副本
那么你在传递裁剪区域坐标的时候,还需传递图片的原始尺寸
换算一下就可以了
x= x/680*图片宽
------解决思路----------------------
可以参考一下我之前写的裁剪。因为你是要裁剪,所以宽和高有一个是和裁剪后的尺寸的宽和高一样,另一边会>=裁剪后的。
http://blog.csdn.net/fdipzone/article/details/9316385
/** 获取目标图生成的size
* @return array $width, $height
*/
private function get_size(){
list($owidth, $oheight) = getimagesize($this->_source);
$width = (int)($this->_width);
$height = (int)($this->_height);
switch($this->_type){
case 'fit':
$pic_w = $width;
$pic_h = (int)($pic_w*$oheight/$owidth);
if($pic_h>$height){
$pic_h = $height;
$pic_w = (int)($pic_h*$owidth/$oheight);
}
break;
case 'crop':
$pic_w = $width;
$pic_h = (int)($pic_w*$oheight/$owidth);
if($pic_h<$height){
$pic_h = $height;
$pic_w = (int)($pic_h*$owidth/$oheight);
}
break;
}
return array($pic_w, $pic_h);
}