【问题标题】:Save image in different formats以不同格式保存图像
【发布时间】:2014-06-10 09:19:16
【问题描述】:

我想根据用户使用 GD 的选择以不同格式保存图像。对于 PNG、JPG 和 GIF 图像,有一个以该格式保存图像的功能。

如何将图像保存为 TIFF、BMP 或 PSD?这些格式是否有可用的功能?

【问题讨论】:

  • 你不能用 GD 做到这一点,你可以使用 ImageMagick。看我的回答here

标签: php image-processing imagemagick gd


【解决方案1】:

GD 无法做到这一点,您需要使用ImageMagick。我假设您已经有 png 格式的图像,并且该图像 url 是 $imageUrl.;

$imageFormat = $_POST["format"];
$imageFormats = array("tiff", "bmp");
if (in_array($imageFormat, $imageFormats)) {
    $handle = fopen($imageUrl, 'rb');
    $imageMagick = new Imagick();
    $imageMagick->readImageFile($handle);
    $imageMagick->setImageFormat($imageFormat);
    $imageMagick->setImageColorSpace(5);
    $imageMagick->writeImage("path_to_tiff_image." . $imageFormat);
    fclose($handle);
    $imageMagick->destroy();    
} else {
    die("Invalid format!");
}

对于 PSD 部分,为什么要将其保存为 psd?没有办法做到这一点。如果你说保存为psd的原因,我可以帮你。

【讨论】:

    【解决方案2】:

    您可以使用 GD 库的内置图像类型和 mime 类型。例如:

    array (IMAGETYPE_PSD        => 'psd',     "mime_type" => 'image/psd'),
    array (IMAGETYPE_BMP        => 'bmp',     "mime_type" => 'image/bmp'),
    array (IMAGETYPE_TIFF_II    => 'tiff',     "mime_type" => 'image/tiff')
    

    【讨论】:

    • 你能分享一个例子吗
    【解决方案3】:
    <?php
        if(!function_exists('image_type_to_extension')){       
    
            $extension;
    
            function image_type_or_mime_type_to_extension($image_type, $include_dot) {
                define ("INVALID_IMAGETYPE", '');
    
                $extension = INVALID_IMAGETYPE;            /// Default return value for invalid input
    
                $image_type_identifiers = array (                                                                ### These values correspond to the IMAGETYPE constants
                        array (IMAGETYPE_GIF         => 'gif',     "mime_type" => 'image/gif'),                        ###  1 = GIF
                        array (IMAGETYPE_JPEG        => 'jpg',     "mime_type" => 'image/jpeg'),                        ###  2 = JPG
                        array (IMAGETYPE_PNG        => 'png',     "mime_type" => 'image/png'),                        ###  3 = PNG
                        array (IMAGETYPE_SWF        => 'swf',     "mime_type" => 'application/x-shockwave-flash'),    ###  4 = SWF  // A. Duplicated MIME type
                        array (IMAGETYPE_PSD        => 'psd',     "mime_type" => 'image/psd'),                        ###  5 = PSD
                        array (IMAGETYPE_BMP        => 'bmp',     "mime_type" => 'image/bmp'),                        ###  6 = BMP
                        array (IMAGETYPE_TIFF_II    => 'tiff',     "mime_type" => 'image/tiff'),                        ###  7 = TIFF (intel byte order)
                        array (IMAGETYPE_TIFF_MM    => 'tiff',     "mime_type" => 'image/tiff'),                        ###  8 = TIFF (motorola byte order)
                        array (IMAGETYPE_JPC        => 'jpc',     "mime_type" => 'application/octet-stream'),            ###  9 = JPC  // B. Duplicated MIME type
                        array (IMAGETYPE_JP2        => 'jp2',     "mime_type" => 'image/jp2'),                        ### 10 = JP2
                        array (IMAGETYPE_JPX        => 'jpf',     "mime_type" => 'application/octet-stream'),            ### 11 = JPX  // B. Duplicated MIME type
                        array (IMAGETYPE_JB2        => 'jb2',     "mime_type" => 'application/octet-stream'),            ### 12 = JB2  // B. Duplicated MIME type            
                        array (IMAGETYPE_SWC        => 'swc',     "mime_type" => 'application/x-shockwave-flash'),    ### 13 = SWC  // A. Duplicated MIME type
                        array (IMAGETYPE_IFF        => 'aiff',     "mime_type" => 'image/iff'),                        ### 14 = IFF
                        array (IMAGETYPE_WBMP        => 'wbmp',     "mime_type" => 'image/vnd.wap.wbmp'),                ### 15 = WBMP
                        array (IMAGETYPE_XBM        => 'xbm',     "mime_type" => 'image/xbm')                            ### 16 = XBM
                );                                                                                    
    
                if((is_int($image_type)) AND (IMAGETYPE_GIF <= $image_type) AND (IMAGETYPE_XBM >= $image_type)){
                    $extension = $image_type_identifiers[$image_type-1]; // -1 because $image_type_identifiers array starts at [0]
                    $extension = $extension[$image_type];
                }
                elseif(is_string($image_type) AND (($image_type != 'application/x-shockwave-flash') OR ($image_type != 'application/octet-stream'))){                
    
                    $extension =  match_mime_type_to_extension($image_type, $image_type_identifiers);
                }
                else
                {
                    $extension = INVALID_IMAGETYPE;
                }
    
                   if(is_bool($include_dot)){
    
                       if((false != $include_dot) AND (INVALID_IMAGETYPE != $extension)){
                           $extension = '.' . $extension;
                       }
                   }
                   else
                   {
                       $extension = INVALID_IMAGETYPE;
                   }                  
    
               return $extension;
    
               }
        }    
    
        function match_mime_type_to_extension($image_type, $image_type_identifiers){
            // Return from loop on a match
            foreach($image_type_identifiers as $_key_outer_loop => $_val_outer_loop){            
                foreach($_val_outer_loop as $_key => $_val){
                    if(is_int ($_key)){             // Keep record of extension for mime check
                        $extension = $_val;
                    } 
                    if($_key == 'mime_type'){    
                        if($_val === $image_type){    // Found match no need to continue looping
                            return $extension;        ### Return
                        }                     
                    }
                }
            }
            // Compared all values without match
            return $extension = INVALID_IMAGETYPE;    
        } 
    
        $extension = image_type_or_mime_type_to_extension($image_type, $include_dot);
         return $extension;
    }
    ?>
    

    【讨论】:

    • OP 询问如何将图像保存为特定格式。此答案中显示的代码仅显示从 MIME 类型返回文件扩展名的代码。我没有看到保存图像的代码。
    猜你喜欢
    • 2012-11-18
    • 2023-03-26
    • 1970-01-01
    • 2018-10-24
    • 1970-01-01
    • 2011-02-17
    • 1970-01-01
    • 1970-01-01
    • 2010-11-23
    相关资源
    最近更新 更多