【问题标题】:How to check and see an images extension using PHP?如何使用 PHP 检查和查看图像扩展?
【发布时间】:2009-11-13 07:41:57
【问题描述】:

如何检查图像是否为 PNG、GIF、TIFF 和 JPG,如果是,则创建缩略图并将其保存到缩略图文件中。

到目前为止,我可以检查并保存 JPEG 图像。

这是下面的代码。

<?php

//Name you want to save your file as
$save = 'members/3/images/thumbs/thumb-pic.jpg';

$file = 'members/3/images/pic.jpg';
echo "Creating file: $save"; 


list($width, $height) = getimagesize($file) ;

if ($width >= 180){
    $modwidth = 180;
    $modheight = ((180.0/$width) * $height);
} else {
    $modwidth = $width;
    $modheight = $height;
}

$tn = imagecreatetruecolor($modwidth, $modheight) ;
$image = imagecreatefromjpeg($file) ;
imagecopyresampled($tn, $image, 0, 0, 0, 0, $modwidth, $modheight, $width, $height) ;

// Here we are saving the .jpg, you can make this gif or png if you want
//the file name is set above, and the quality is set to 100%
imagejpeg($tn, $save, 100) ; 
?> 

【问题讨论】:

    标签: php


    【解决方案1】:

    这是一种方法,使用关联数组将每种格式分配给适当的imagecreatefrom... 函数:

    $handlers = array(
        'jpg'  => 'imagecreatefromjpeg',
        'jpeg' => 'imagecreatefromjpeg',
        'png'  => 'imagecreatefrompng',
        'gif'  => 'imagecreatefromgif'
    );
    
    $extension = strtolower(substr($file, strrpos($file, '.')+1));
    if ($handler = $handlers[$extension]){
        $image = $handler($file);
        //do the rest of your thumbnail stuff here
    }else{
        //throw an 'invalid image' error
    }
    

    【讨论】:

      【解决方案2】:

      我会使用 imagemagick 模块,特别是 Imagick::identifyImage() 方法。它返回如下数组 - 检查 format 键中的内容。

      Array
      (
          [imageName] => /some/path/image.jpg
          [format] => JPEG (Joint Photographic Experts Group JFIF format)
          [geometry] => Array
              (
                  [width] => 90
                  [height] => 90
              )
      
          [type] => TrueColor
          [colorSpace] => RGB
          [resolution] => Array
              (
                  [x] => 300
                  [y] => 300
              )
      
          [units] => PixelsPerInch
          [fileSize] => 1.88672kb
          [compression] => JPEG
          [signature] => 9a6dc8f604f97d0d691c0286176ddf992e188f0bebba98494b2146ee2d7118da
      )
      

      【讨论】:

      • 依靠 imagick 来完成像生成缩略图这样简单的事情有点粗略……我接触过的主机并不多。
      【解决方案3】:

      你基本上已经搞定了。

      使用exif_image type 或pathinfo($image_path, PATHINFO_EXTENSION) 获取文件类型。然后将其保存到变量中(例如 $type)。

      然后,使用您设置的文件类型变量作为开关比较,使用 switch 语句运行适当的 imagefrom* 函数(查看imagefromjpeg 页面“另请参阅”以获取列表)。

      最后,使用相同的基本 switch 语句运行 image*(imagepng 等 [在imagejpeg 页面上查看它们])以将拇指保存回原始类型(这对于png 和 gif 可能具有透明度,在这种情况下您需要启用透明度的保存)。

      顺便说一句,对于透明度,请查看 imagesavealpha 和 imagealphablending。

      【讨论】:

        【解决方案4】:

        您可以使用 mime 函数。试试echo mime_content_type('php.gif'):

        【讨论】:

          【解决方案5】:

          我会按照 brianreavis 的回答这样做,但我不会依赖图像文件的扩展名来确定图像类型,而是使用 exif_imagetype 函数。

          $filetypeinfo = exif_imagetype($file);
          switch($filetypeinfo){
              case 1:
              $extension = 'gif';
              break;
              case 2:
              $extension = 'jpg';
              break;
              case 3:
              $extension = 'png';
              break;
          }
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2010-10-03
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2016-05-30
            • 2011-11-13
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多