我想用来自的imagick代码替换所有的GD代码 "裁剪jquery插件"来自www.croppic.net
这是原始文件 https://github.com/sconsult/croppic/blob/master/img_crop_to_file.php
我做了什么
// 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'];
//quali
$jpeg_quality = 100;
/* resize the original image to size of editor */
// $imgUrl = '/kunden/40/0_org.jpg';
// $img_r = imagecreatefromjpeg($imgUrl);
// $source_image = imagecreatefromjpeg($imgUrl);
// $type = '.jpeg';
// $resizedImage = imagecreatetruecolor($imgW, $imgH);
// imagecopyresampled($resizedImage, $source_image, 0, 0, 0, 0, $imgW, $imgH, $imgInitW, $imgInitH);
$im = new imagick('/kunden/40/0_org.jpg');
$im->resizeImage($imgW, $imgH, 0, 0, false);
/* rotate the rezized image */
// $rotated_image = imagerotate($resizedImage, -$angle, 0);
$im->rotateimage('#fff', -$angle);
/* find new width & height of rotated image */
// $rotated_width = imagesx($rotated_image);
// $rotated_height = imagesy($rotated_image);
$d = $im->getImageGeometry();
$rotated_width = $d['width'];
$rotated_height = $d['height'];
/* diff between rotated & original sizes */
$dx = $rotated_width - $imgW;
$dy = $rotated_height - $imgH;
/* crop rotated image to fit into original rezized rectangle */
// $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);
$im->cropImage($imgW,$imgH, $dx/2, $dy/2); // i think this is not correct
/* 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);
/* save image */
// imagejpeg($final_image, $output_filename.'.jpg', $jpeg_quality);
$im->writeImage('/kunden/40/0.jpg');
结果是黑色图像的宽度和高度不正确。 希望有人可以帮助我。提前谢谢!
答案 0 :(得分:4)
问题与resizeImage方法一致。 请注意第3和第4个参数:int $ filter,float $ blur。您可以找到更多详细信息here
尝试使用以下内容替换此行以获得正确的结果:
$im->resizeImage($imgW, $imgH, imagick::FILTER_LANCZOS, 1, false);
您可以使用不同过滤器here
进行演示答案 1 :(得分:1)
这是新图片的宽度和高度。
$new_width = $max_width = $img_width = $image->getImageWidth();
$new_height = $max_height = $img_height = $image->getImageHeight();
请按照以下步骤操作
if (($img_width / $img_height) >= ($max_width / $max_height)) {
$new_width = 0; // Enables proportional scaling based on max_height
$x = ($img_width / ($img_height / $max_height) - $max_width) / 2;
} else {
$new_height = 0; // Enables proportional scaling based on max_width
$y = ($img_height / ($img_width / $max_width) - $max_height) / 2;
}
,最后一步
$success = $image->resizeImage(
$new_width,
$new_height,
isset($options['filter']) ? $options['filter'] : imagick::FILTER_LANCZOS,
isset($options['blur']) ? $options['blur'] : 1,
$new_width && $new_height // fit image into constraints if not to be cropped
);
if ($success && $crop) {
$success = $image->cropImage(
$max_width,
$max_height,
$x,
$y
);
if ($success) {
$success = $image->setImagePage($max_width, $max_height, 0, 0);
}
}
$type = strtolower(substr(strrchr($file_name, '.'), 1));
switch ($type) {
case 'jpg':
case 'jpeg':
if (!empty($options['jpeg_quality'])) {
$image->setImageCompression(Imagick::COMPRESSION_JPEG);
$image->setImageCompressionQuality($options['jpeg_quality']);
}
break;
}
if (!empty($options['strip'])) {
$image->stripImage();
}
return $success && $image->writeImage($new_file_path);
这是我的代码:)尝试和播放