【问题标题】:Cropping is done to the left side of image裁剪是在图像的左侧完成的
【发布时间】:2016-10-18 22:47:40
【问题描述】:

我正在使用代码进行裁剪

$filename = "thimg.jpg";

// Get dimensions of the coriginal image
list($width, $height) = getimagesize($filename);

// Resample the image
$canvas = imagecreatetruecolor('759', '599');
$current_image = imagecreatefromjpeg($filename);
imagecopy($canvas, $current_image, 0, 0, $width/8, $height/8, '759', '599');
imagejpeg($canvas, $filename.'_cropped.jpg', 100);
chmod($filename.'_cropped.jpg', 0644);
unlink($filename);

但它在图像的左侧而不是中间被裁剪。

请提供建议。

【问题讨论】:

    标签: php thumbnails gd crop image-uploading


    【解决方案1】:

    $width/8$height/8 的结果不适用于居中裁剪。

    你需要计算:

    (original_width ÷ 2) - (target_width ÷ 2)

    和:

    (original_height ÷ 2) - (target_height ÷ 2)

    在您的具体情况下,如下所示:

    imagecopy($canvas, $current_image, 0, 0, ($width/2)-(759/2), ($height/2)-(599/2), 759, 599);
    

    还请注意,您应该将大小值作为整数 (759, 599) 传递,而不是字符串 ('759', '599')

    【讨论】:

    猜你喜欢
    • 2013-08-28
    • 1970-01-01
    • 2021-03-31
    • 1970-01-01
    • 1970-01-01
    • 2019-03-12
    • 2017-05-22
    • 1970-01-01
    • 2013-04-14
    相关资源
    最近更新 更多