【发布时间】:2019-06-29 07:59:22
【问题描述】:
现在我的代码为我提供了一个具有正确按比例缩小的高度和宽度的图像,但使用了原始图像的顶部。我希望它使用图像的中心。但我不知道如何计算 imagecopyresampled 的 dst_y 值来执行此操作。谁能帮我解决:-)
<?php
function ReSize ($source,$destination,$dest_imagex,$dest_imagey,$quality) {
$source_image = imagecreatefromjpeg($source);
// Get dimensions of the original image
$source_imagex = imagesx($source_image);
$source_imagey = imagesy($source_image);
$after_width = $dest_imagex;
//get the reduced width
$reduced_width = ($source_imagex - $after_width);
//now convert the reduced width to a percentage and round it to 2 decimal places
$reduced_radio = round(($reduced_width / $source_imagex) * 100, 2);
//ALL GOOD! let's reduce the same percentage from the height and round it to 2 decimal places.
$reduced_height = round(($source_imagey / 100) * $reduced_radio, 2);
//reduce the calculated height from the original height
$after_height = $source_imagey - $reduced_height;
$dst_y = 0; // The calculation I cannot figure out.....
$dest_image = imagecreatetruecolor($dest_imagex, $dest_imagey);
imagecopyresampled($dest_image, $source_image, 0, $dst_y, 0, 0, $after_width, $after_height, $source_imagex, $source_imagey);
$cache_folder = 'images/cache/';
$new_image = $cache_folder . rawurlencode($destination). '_' . $dest_imagex . 'x' . $dest_imagey . '.jpg';
imagejpeg($dest_image, $new_image,$quality);
echo $new_image;
}
?>
【问题讨论】:
标签: php image image-processing