3

I am new to php and trying to learn about image upscaling. can you please show me how can I upscale an image to a certain size? For example, I would like to re-size the following image while keeping the aspect ratio. If this can be done, can you please show me with an example enter image description here

4

4 回答 4

3
$source_image = imagecreatefromjpeg("osaka.jpg");
$source_imagex = imagesx($source_image);
$source_imagey = imagesy($source_image);
$dest_imagex = 300;
$dest_imagey = 200;
$dest_image = imagecreatetruecolor($dest_imagex, $dest_imagey);

质量差,但速度快:

imagecopyresized($dest_image, $source_image, 0, 0, 0, 0, 
            $dest_imagex, $dest_imagey, $source_imagex, $source_imagey);

最好的质量,但速度慢:

imagecopyresampled($dest_image, $source_image, 0, 0, 0, 0, 
            $dest_imagex, $dest_imagey, $source_imagex, $source_imagey);
于 2013-08-08T07:35:40.540 回答
1

您可以通过将一侧设置为自定义长度并将另一侧设置为“自动”来放大 html/CSS 中的图像。无需在 php 中对其进行升级,因为升级后的图像不包含比“正常”版本更多的信息。它只会消耗更多的带宽。

<img src="http://placekitten.com/50/50" style="width: 50px; height: auto;" />

<img src="http://placekitten.com/50/50" style="width: 200px; height: auto;" />

看到这个小提琴

于 2013-08-08T07:36:13.103 回答
0

如果你真的想用 PHP 来做,你可以看看“imagecopyresampled”函数:

php.net 上的 imagecopyresampled 示例

但是如果你想保持比例,你必须计算它并手动应用到宽度和高度。

于 2013-08-08T07:34:41.893 回答
0

图像放大

// creating image resource from image path (also supports url)
$source_image = imagecreatefromjpeg($destinationReal); // 1150x235 px

// getting source image dimension
$source_imagex = imagesx($source_image);
$source_imagey = imagesy($source_image);

// set new image dimension
$dest_imagex = 1150;
$dest_imagey = 348; // in my case I only upscale the height

// create image (in memory)
$dest_image = imagecreatetruecolor($dest_imagex, $dest_imagey);
imagecopyresampled($dest_image, $source_image, 0, 0, 0, 0,
                $dest_imagex, $dest_imagey, $source_imagex, $source_imagey);

// save new image
imagejpeg($dest_image, $destination);
于 2020-04-15T12:24:58.597 回答