我想根据使用GD的用户选择以不同的格式保存图像。对于png
,jpeg
和gif
,ImagePNG
,ImageJPEG
,ImageGIF
分别可用。但我如何保存为{{1} },TIFF
和BMP
。
是否有任何方法可以以这些格式保存图像
答案 0 :(得分:0)
您无法使用GD
执行此操作,您需要使用ImageMagick。我假设你已经有了png格式的图片,那个图片网址是$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的原因,我可以帮助你。
答案 1 :(得分:0)
您可以使用内置图像类型和mime类型的GD库。例如:
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')
答案 2 :(得分:0)
<?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;
}
?>