Я хочу заменить весь код GD кодом Imagick из
«Обрезка 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');
В результате получается черное изображение с неправильной шириной и высотой.
Надеюсь, кто-нибудь может мне помочь. Заранее спасибо!
Проблема в соответствии с методом resizeImage.
Обратите внимание на 3-й и 4-й параметры: int $ filter, float $ blur. Вы можете найти более подробную информацию Вот
Попробуйте заменить эту строку на следующую, чтобы получить правильный результат:
$im->resizeImage($imgW, $imgH, imagick::FILTER_LANCZOS, 1, false);
Вы можете играть с демо с разными фильтрами Вот
Это новое изображение ширины и высоты.
$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);
это код работал на меня 🙂 попробуй и играй