PHP 品质预览照片
借助什么可以实现与预览 vkontakte.ru (而不是 adv)相同质量的图片?
我使用 GD 库。
图像质量 vkontakte:
我的脚本的质量图片:
大照片:链接
在所有这张照片中,质量最好的 vkontakte 重 7Kb,而我的 16K ...
我的脚本:
<?php
function _makeThumbnail($image, $dest, $ext)
{
$imageType = exif_imagetype($image);
switch ($imageType)
{
case IMAGETYPE_JPEG:
$img = imagecreatefromjpeg($image);
break;
case IMAGETYPE_PNG:
$img = imagecreatefrompng($image);
break;
case IMAGETYPE_GIF:
$img = imagecreatefromgif($image);
break;
default:
throw new Exception('Bad extension');
}
$width = imagesx($img);
$height = imagesy($img);
list($widthX, $heightX) = array('130', '130');
if ($width > $widthX || $height > $heightX)
{
if ($height > $width)
{
$ratio = $heightX / $height;
$newHeight = $heightX;
$newWidth = $width * $ratio;
}
else
{
$ratio = $widthX / $width;
$newWidth = $widthX;
$newHeight = $height * $ratio;
}
$previewImg = imagecreatetruecolor($newWidth, $newHeight);
$palsize = ImageColorsTotal($img);
for ($i = 0; $i < $palsize; $i++)
{
$colors = ImageColorsForIndex($img, $i);
ImageColorAllocate($previewImg, $colors['red'], $colors['green'], $colors['blue']);
}
imagecopyresized($previewImg, $img, 0, 0, 0, 0, $newWidth, $newHeight, $width, $height);
$name = $dest;
switch ($imageType)
{
case IMAGETYPE_JPEG:
imagejpeg($previewImg, $name . '.' . $ext, 100);
break;
case IMAGETYPE_PNG:
imagesavealpha($previewImg, true);
imagepng($previewImg, $name . '.' . $ext, 9);
case IMAGETYPE_GIF:
imagegif($previewImg, $name . '.' . $ext);
break;
default:
throw new Exception();
}
}
imagedestroy($previewImg);
imagedestroy($img);
}
实际上需要解决两个问题。 制作最佳质量,从而减小预览的大小。
With the help of what can be achieved with the same quality pictures as a preview vkontakte.ru (not adv)?
I use the library GD.
Image quality vkontakte:
Quality pictures to my script:
Big photo: Link
In all of this photo vkontakte with the best quality weighs 7Kb, and my 16K ...
My script:
<?php
function _makeThumbnail($image, $dest, $ext)
{
$imageType = exif_imagetype($image);
switch ($imageType)
{
case IMAGETYPE_JPEG:
$img = imagecreatefromjpeg($image);
break;
case IMAGETYPE_PNG:
$img = imagecreatefrompng($image);
break;
case IMAGETYPE_GIF:
$img = imagecreatefromgif($image);
break;
default:
throw new Exception('Bad extension');
}
$width = imagesx($img);
$height = imagesy($img);
list($widthX, $heightX) = array('130', '130');
if ($width > $widthX || $height > $heightX)
{
if ($height > $width)
{
$ratio = $heightX / $height;
$newHeight = $heightX;
$newWidth = $width * $ratio;
}
else
{
$ratio = $widthX / $width;
$newWidth = $widthX;
$newHeight = $height * $ratio;
}
$previewImg = imagecreatetruecolor($newWidth, $newHeight);
$palsize = ImageColorsTotal($img);
for ($i = 0; $i < $palsize; $i++)
{
$colors = ImageColorsForIndex($img, $i);
ImageColorAllocate($previewImg, $colors['red'], $colors['green'], $colors['blue']);
}
imagecopyresized($previewImg, $img, 0, 0, 0, 0, $newWidth, $newHeight, $width, $height);
$name = $dest;
switch ($imageType)
{
case IMAGETYPE_JPEG:
imagejpeg($previewImg, $name . '.' . $ext, 100);
break;
case IMAGETYPE_PNG:
imagesavealpha($previewImg, true);
imagepng($previewImg, $name . '.' . $ext, 9);
case IMAGETYPE_GIF:
imagegif($previewImg, $name . '.' . $ext);
break;
default:
throw new Exception();
}
}
imagedestroy($previewImg);
imagedestroy($img);
}
Actually necessary to solve two problems.
Make the best quality and thus reduce the size of the preview.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用
imagecopyresampled()
而不是imagecopyresized()
通常可以解决尖锐的问题。也就是说,无论以任何标准衡量,GD 的 JPG 压缩都不是很好。就图像质量与文件大小而言,它远不及 Photoshop 出色的导出过滤器。 ImageMagick 往往至少稍微好一些 - 如果良好的压缩非常重要,那么也许值得一看。
Using
imagecopyresampled()
instead ofimagecopyresized()
usually solves the acute problem.That said, GD's JPG compression is not great by any standard. It is nowhere near Photoshop's excellent export filter in terms of image quality vs. file size. ImageMagick tends to be at least slightly better - if good compression is very important, it maybe is worth a look.
@Pekka 是正确的,对于第二个问题,您可以使用 imagejpeg($img, $filename, $quality) 来优化缩略图。
这里是一个指南和一些示例,可以帮助您解决这个问题。
@Pekka is correct, and as for the second issue you can use
imagejpeg($img, $filename, $quality)
to optimize the thumbnail.here is a guide and some samples to help you out with that.