gpt4 book ai didi

php - 图片缩放不变形

转载 作者:搜寻专家 更新时间:2023-10-31 20:39:24 25 4
gpt4 key购买 nike

我正在尝试使用 gd 库创建缩略图,但我的缩略图总是变形。我想要的是从中心开始切割图像,尊重新的分辨率并失去旧的比例。

我尝试过的:

$im = ImageCreateFromJPEG($target_file);
$n_width = 500; // Fix the width of the thumb nail images
$n_height = 500; // Fix the height of the thumb nail imaage
$width = ImageSx($im); // Original picture width is stored
$height = ImageSy($im); // Original picture height is stored
$newimage = imagecreatetruecolor($n_width, $n_height);
imagecopyresampled($newimage, $im, 0, 0, 0, 0, $n_width, $n_height, $width, $height);
$thumb_target = $target_dir . $filename_without_ext . '-thumb.' . $params['file_ext'];
ImageJpeg($newimage, $thumb_target);
chmod("$thumb_target", 0777);

尝试将 imagecreatetruecolor 更改为 imagecrop 但仍然没有我想要的行为。

如果我不够清楚,请告诉我。

最佳答案

使用 ImageManipulator 解决了它图书馆。在 this answer 中找到解决方案.

我的最终代码:

$im = new ImageManipulator($target_file);
$centreX = round($im->getWidth() / 2);
$centreY = round($im->getHeight() / 2);

$x1 = $centreX - 500;
$y1 = $centreY - 500;

$x2 = $centreX + 500;
$y2 = $centreY + 500;

$im->crop($x1, $y1, $x2, $y2);
$im->save($target_dir . $filename_without_ext . '-thumb.' . $params['file_ext']);

关于php - 图片缩放不变形,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26908597/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com