本文介绍了裁剪图像croppic + CI时出现错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用croppic来裁剪图像. 错误意外令牌< 时,上传图片成功,但是裁剪图片时不起作用.它无法获取$ ImgUrl.值这是我当前的代码:
Im using croppic to crop images. Its succesfull when upload image, but its not working when crop images, error unexpected token <. It cant get $ImgUrl. value Here is my current code:
function image_cropping(){
$imgUrl = $_POST['imgUrl'];
// original sizes
$imgInitW = $_POST['imgInitW'];
$imgInitH = $_POST['imgInitH'];
// resized sizes
$imgW = $_POST['imgW'];
$imgH = $_POST['imgH'];
// offsets
$imgY1 = $_POST['imgY1'];
$imgX1 = $_POST['imgX1'];
// crop box
$cropW = $_POST['cropW'];
$cropH = $_POST['cropH'];
// rotation angle
$angle = $_POST['rotation'];
$jpeg_quality = 100;
$output_filename =base_url() . 'assets/images/croppedImg_'.rand();
$what = getimagesize($imgUrl);
switch(strtolower($what['mime']))
{
case 'image/png':
$img_r = imagecreatefrompng($imgUrl);
$source_image = imagecreatefrompng($imgUrl);
$type = '.png';
break;
case 'image/jpeg':
$img_r = imagecreatefromjpeg($imgUrl);
$source_image = imagecreatefromjpeg($imgUrl);
error_log("jpg");
$type = '.jpeg';
break;
case 'image/gif':
$img_r = imagecreatefromgif($imgUrl);
$source_image = imagecreatefromgif($imgUrl);
$type = '.gif';
break;
default: die('image type not supported');
}
if(!is_writable(dirname($output_filename))){
$response = Array(
"status" => 'error',
"message" => 'Can`t write cropped File'
);
}else{
$resizedImage = imagecreatetruecolor($imgW, $imgH);
imagecopyresampled($resizedImage, $source_image, 0, 0, 0, 0, $imgW, $imgH, $imgInitW, $imgInitH);
$rotated_image = imagerotate($resizedImage, -$angle, 0);
$rotated_width = imagesx($rotated_image);
$rotated_height = imagesy($rotated_image);
$dx = $rotated_width - $imgW;
$dy = $rotated_height - $imgH;
$cropped_rotated_image = imagecreatetruecolor($imgW, $imgH);
imagecolortransparent($cropped_rotated_image, imagecolorallocate($cropped_rotated_image, 0, 0, 0));
imagecopyresampled($cropped_rotated_image, $rotated_image, 0, 0, $dx / 2, $dy / 2, $imgW, $imgH, $imgW, $imgH);
// crop image into selected area
$final_image = imagecreatetruecolor($cropW, $cropH);
imagecolortransparent($final_image, imagecolorallocate($final_image, 0, 0, 0));
imagecopyresampled($final_image, $cropped_rotated_image, 0, 0, $imgX1, $imgY1, $cropW, $cropH, $cropW, $cropH);
imagejpeg($final_image, $output_filename.$type, $jpeg_quality);
$response = Array(
"status" => 'success',
"url" => $output_filename.$type
);
}
print json_encode($response);
}
}
任何想法出了什么问题?谢谢
any ideas whats going wrong?thank you
推荐答案
您可以从此链接下载源文件 http://imakeitsolutions.com/croppic/
You can download the source file from this link http://imakeitsolutions.com/croppic/
尝试使用此代码
imagejpeg($final_image, $output_filename.$type, $jpeg_quality);
$response = Array(
"status" => 'success',
"url" => base_url(). $output_filename.$type
);
这篇关于裁剪图像croppic + CI时出现错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!