-2

我正在尝试创建图像的缩略图,但我发现使用下面的代码只能将 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);


    }
}
4

2 回答 2

1

您面临的问题是由于这一行:

// save thumbnail into a file
imagejpeg( $tmp_img, "{$pathToThumbs}{$fname}" );

PHP imagejpeg 将图像保存为 Jpeg。

您需要的是利用适当的功能来保存每种文件类型。另外您需要控制输出文件类型扩展名,如果不是联合图像专家组文件,.jpeg将不会被占用。

处理文件扩展名

PHP image_type_to_extension的注释中有一些很好的例子!

处理保存过程

图像wbmp

// Save the image as a WBMP
imagewbmp( $tmp_img, "{$pathToThumbs}{$fname}" . '.wbmp' );

imagewbmp()输出或保存给定图像的 WBMP 版本。

图像gif

// Save the image as a GIF
imagegif( $tmp_img, "{$pathToThumbs}{$fname}" . '.gif' );

imagegif()从图像图像中创建文件名中的 GIF 文件。

图像png

// Save the image as a PNG
imagepng( $tmp_img, "{$pathToThumbs}{$fname}" . '.png' );

imagepng — 将 PNG 图像输出到浏览器或文件。

于 2012-06-05T07:17:31.553 回答
-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);
于 2012-06-15T10:20:40.717 回答