我正在尝试创建图像的缩略图,但我发现只有使用下面的代码才能将JPG图像转换为缩略图;它不适用于PNG和GIF。我做错了什么?
function createThumbs( $pathToImages,$pathToThumbs,$thumbWidth,$fname)
{
// open the directory
// $dir = opendir( $pathToImages );
// parse path for the extension
$info = pathinfo($pathToImages);
// continue only if this is a JPEG image
//if ( strtolower($info['extension']) == 'jpg' )
//{
//echo "Creating thumbnail for {$fname} <br />";
echo strtolower($info['extension']);
switch(strtolower($info['extension'])){
case 'jpg':
$img = imagecreatefromjpeg("{$pathToImages}" );
break;
case 'gif':
$img = imagecreatefromgif("{$pathToImages}" );
break;
case 'png':
$img = imagecreatefrompng("{$pathToImages}" );
break;
default:
die("ERROR: FILE TYPE DOES NOT SUPPORT");
break;
}
// load image and get image size
$width = imagesx( $img );
$height = imagesy( $img );
// calculate thumbnail size
$new_width = $thumbWidth;
$new_height = floor( $height * ( $thumbWidth / $width ) );
// create a new temporary image
$tmp_img = imagecreatetruecolor( $new_width, $new_height );
// copy and resize old image into new image
imagecopyresized( $tmp_img, $img, 0, 0, 0, 0, $new_width, $new_height, $width, $height );
// save thumbnail into a file
imagejpeg( $tmp_img, "{$pathToThumbs}{$fname}" );
chmod("{$pathToThumbs}{$fname}",777);
}
}
答案 0 :(得分:1)
您面临的问题是由于这一行:
// save thumbnail into a file
imagejpeg( $tmp_img, "{$pathToThumbs}{$fname}" );
PHP imagejpeg会将图像保存为Jpeg。
您需要的是利用适当的功能来保存每种文件类型。您还需要控制输出文件类型扩展名,如果不是联合图像专家组文件,.jpeg
将不会被占用。
PHP image_type_to_extension的评论中有一些很好的例子!
<强> imagewbmp 强>
// Save the image as a WBMP
imagewbmp( $tmp_img, "{$pathToThumbs}{$fname}" . '.wbmp' );
imagewbmp()输出或保存给定图像的WBMP版本。
<强> imagegif 强>
// Save the image as a GIF
imagegif( $tmp_img, "{$pathToThumbs}{$fname}" . '.gif' );
imagegif()从图片图片中创建文件名中的GIF文件。
<强> imagepng 强>
// Save the image as a PNG
imagepng( $tmp_img, "{$pathToThumbs}{$fname}" . '.png' );
imagepng - 将PNG图像输出到浏览器或文件。
答案 1 :(得分:-1)
$new_images = $_FILES["profilepic"]["name"];
$width = 400; //*** Fix Width & Heigh (Autu caculate) ***//
$size = GetimageSize($images);
$height = round($width * $size[1] / $size[0]);
list($origwidth, $origheight, $origtype, $origattr) = getimagesize($images);
$origfiletype = image_type_to_extension($origtype, true);
// $filetype includes the dot.
$supported = array('.jpeg', '.png', '.gif');
//check file type
if (in_array($origfiletype, $supported)) {
if ('.jpeg' == $origfiletype) {
$images_orig = ImageCreateFromJPEG($images);
} else if ('.png' == $origfiletype) {
try {
$images_orig = ImagecreateFromPNG($images);
} catch (Exception $e) {
echo $e->getMessage();
exit;
}
} else if ('.gif' == $origfiletype) {
try {
$images_orig = ImagecreateFromGIF($images);
} catch (Exception $e) {
echo $e->getMessage();
exit;
}
}
$photoX = ImagesX($images_orig);
$photoY = ImagesY($images_orig);
$images_fin = ImageCreateTrueColor($width, $height);
ImageCopyResampled($images_fin, $images_orig, 0, 0, 0, 0, $width + 1, $height + 1, $photoX, $photoY);
ImageJPEG($images_fin, dirname(__FILE__) . "/gallery/" . $new_images, $jpeg_quality);
ImageDestroy($images_orig);
ImageDestroy($images_fin);