为什么这位受欢迎的作者将所有图像转换为 .jpg?
下面的代码是根据 O'Reilly 书籍《学习 PHP、MySQL 和 JavaScript》修改而来的,可以在此处找到
该书所有图像类型都转换为 .jpg 吗?
.jpg 是否提供最佳质量/尺寸比?
public static function upload()
{
$email=$_SESSION['email'];
$path1="i8.jpg";
$path2="z_p/$email.jpg";
$path3="i9.jpg";
$path4="z_p/$email-1.jpg";
if(move_uploaded_file($_FILES['ufile']['tmp_name'], $path2))
{
$typeok=TRUE;
switch($_FILES['ufile']['type'])
{
case "image/gif":
$src = imagecreatefromgif($path2);
break;
case "image/jpeg":
case "image/pjpeg":
$src = imagecreatefromjpeg($path2);
break;
case "image/png":
$src = imagecreatefrompng($path2);
break;
default:
$typeok = FALSE;
break;
}
if($typeok)
{
list($w, $h) = getimagesize($path2);
$tw = $w;
$th = $h;
/*Run 1*/
$max = 50;
if($w > $h && $max < $w)
{
$th = $max / $w * $h;
$tw = $max;
}
elseif ($h > $w && $max < $h)
{
$tw = $max / $h * $w;
$th = $max;
}
elseif ($max < $w)
{
$tw = $th = $max;
}
$dst = imagecreatetruecolor($tw, $th);
imagecopyresampled($dst, $src, 0, 0, 0, 0, $tw, $th, $w, $h);
imagejpeg($dst, $path2);
imagedestroy($dst);
/* Rune 2 */
$max = 20;
if($w > $h && $max < $w)
{
$th = $max / $w * $h;
$tw = $max;
}
elseif ($h > $w && $max < $h)
{
$tw = $max / $h * $w;
$th = $max;
}
elseif ($max < $w)
{
$tw = $th = $max;
}
$dst = imagecreatetruecolor($tw, $th);
imagecopyresampled($dst, $src, 0, 0, 0, 0, $tw, $th, $w, $h);
imagejpeg($dst, $path4);
imagedestroy($dst);
imagedestroy($src);
}
}
else
{
copy($path1, $path2);
copy($path3, $path4);
}
}
The code below is modified from an O'Reilly Book - Learning PHP, MySQL, and JavaScript which can be found here
Why are all image types converted to .jpg?
Does .jpg offer the best quality/size ratio?
public static function upload()
{
$email=$_SESSION['email'];
$path1="i8.jpg";
$path2="z_p/$email.jpg";
$path3="i9.jpg";
$path4="z_p/$email-1.jpg";
if(move_uploaded_file($_FILES['ufile']['tmp_name'], $path2))
{
$typeok=TRUE;
switch($_FILES['ufile']['type'])
{
case "image/gif":
$src = imagecreatefromgif($path2);
break;
case "image/jpeg":
case "image/pjpeg":
$src = imagecreatefromjpeg($path2);
break;
case "image/png":
$src = imagecreatefrompng($path2);
break;
default:
$typeok = FALSE;
break;
}
if($typeok)
{
list($w, $h) = getimagesize($path2);
$tw = $w;
$th = $h;
/*Run 1*/
$max = 50;
if($w > $h && $max < $w)
{
$th = $max / $w * $h;
$tw = $max;
}
elseif ($h > $w && $max < $h)
{
$tw = $max / $h * $w;
$th = $max;
}
elseif ($max < $w)
{
$tw = $th = $max;
}
$dst = imagecreatetruecolor($tw, $th);
imagecopyresampled($dst, $src, 0, 0, 0, 0, $tw, $th, $w, $h);
imagejpeg($dst, $path2);
imagedestroy($dst);
/* Rune 2 */
$max = 20;
if($w > $h && $max < $w)
{
$th = $max / $w * $h;
$tw = $max;
}
elseif ($h > $w && $max < $h)
{
$tw = $max / $h * $w;
$th = $max;
}
elseif ($max < $w)
{
$tw = $th = $max;
}
$dst = imagecreatetruecolor($tw, $th);
imagecopyresampled($dst, $src, 0, 0, 0, 0, $tw, $th, $w, $h);
imagejpeg($dst, $path4);
imagedestroy($dst);
imagedestroy($src);
}
}
else
{
copy($path1, $path2);
copy($path3, $path4);
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
jpeg 文件(或任何一种图像格式)中可以隐藏许多令人讨厌的惊喜。通过始终以这种方式重新创建映像,您可以确信服务器发出的映像已被清理。
There are lots of nasty surprises you can hide inside a jpeg file (or any of a number of image formats). By always recreating an image this way, you gain a certain amount of confidence that the image your server issues is sanitized.
JPEG并不总是提供最佳的尺寸/质量比,这取决于图像内容,如果它有很多颜色和很多渐变,或者是风景图片,JPEG可能是最好的选择,但对于像屏幕截图这样的东西PNG将提供最佳尺寸/质量比。
JPEG doesn't always offer the best size/quality ratio, It depends on the image content, if it has many colors with many gradients, or is scenery picture, JPEG may be the best option, but for something like screenshots PNG will offer the best size/quality ratio.