为什么这位受欢迎的作者将所有图像转换为 .jpg?

发布于 2024-12-03 20:07:14 字数 2314 浏览 0 评论 0原文

下面的代码是根据 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

心头的小情儿 2024-12-10 20:07:14

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.

挽你眉间 2024-12-10 20:07:14

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文