У меня есть следующий код для создания миниатюры изображения, но он работает только для JPEG по неизвестной причине, я не могу понять, почему он не работает для PNG?

function generate_image_thumbnail($source_image_path, $thumbnail_image_path)
    {
        list($source_image_width, $source_image_height, $source_image_type) = getimagesize($source_image_path);
        switch ($source_image_type) {
            case IMAGETYPE_GIF:
                $source_gd_image = imagecreatefromgif($source_image_path);
                break;
            case IMAGETYPE_JPEG:
                $source_gd_image = imagecreatefromjpeg($source_image_path);
                break;
            case IMAGETYPE_PNG:{
                $source_gd_image = imagecreatefrompng($source_image_path);
                break;
            }
        } //$source_image_type
        if ($source_gd_image === false) {
            return false;
        } //$source_gd_image === false
        $source_aspect_ratio    = $source_image_width / $source_image_height;
        $thumbnail_aspect_ratio = THUMBNAIL_IMAGE_MAX_WIDTH / THUMBNAIL_IMAGE_MAX_HEIGHT;
        if ($source_image_width <= THUMBNAIL_IMAGE_MAX_WIDTH && $source_image_height <= THUMBNAIL_IMAGE_MAX_HEIGHT) {
            $thumbnail_image_width  = $source_image_width;
            $thumbnail_image_height = $source_image_height;
        } //$source_image_width <= THUMBNAIL_IMAGE_MAX_WIDTH && $source_image_height <= THUMBNAIL_IMAGE_MAX_HEIGHT
        elseif ($thumbnail_aspect_ratio > $source_aspect_ratio) {
            $thumbnail_image_width  = (int) (THUMBNAIL_IMAGE_MAX_HEIGHT * $source_aspect_ratio);
            $thumbnail_image_height = THUMBNAIL_IMAGE_MAX_HEIGHT;
        } //$thumbnail_aspect_ratio > $source_aspect_ratio
        else {
            $thumbnail_image_width  = THUMBNAIL_IMAGE_MAX_WIDTH;
            $thumbnail_image_height = (int) (THUMBNAIL_IMAGE_MAX_WIDTH / $source_aspect_ratio);
        }
        $thumbnail_gd_image = imagecreatetruecolor($thumbnail_image_width, $thumbnail_image_height);
        imagecopyresampled($thumbnail_gd_image, $source_gd_image, 0, 0, 0, 0, $thumbnail_image_width, $thumbnail_image_height, $source_image_width, $source_image_height);

        switch ($source_image_type) {
            case IMAGETYPE_GIF:
                imagegif($thumbnail_gd_image, $thumbnail_image_path, 90);
                break;
            case IMAGETYPE_JPEG:
                imagejpeg($thumbnail_gd_image, $thumbnail_image_path, 90);
                break;
            case IMAGETYPE_PNG:{
                //imagealphablending($thumbnail_gd_image, false); 
                //imagesavealpha($thumbnail_gd_image,true);
                imagepng($thumbnail_gd_image, $thumbnail_image_path, 90);
                break;
            }
        }               



        imagedestroy($source_gd_image);
        imagedestroy($thumbnail_gd_image);
        return true;
    }
-1
mahen3d 19 Мар 2014 в 06:03

2 ответа

Лучший ответ

Проблема во втором случае png, именно в

imagepng($thumbnail_gd_image, $thumbnail_image_path, 90);

Согласно функции imagepng

качество

Compression level: from 0 (no compression) to 9.

Итак, измените 90 на 9 (например)

imagepng($thumbnail_gd_image, $thumbnail_image_path, 9);
1
kraysak 19 Мар 2014 в 03:42
imagepng($thumbnail_gd_image, $thumbnail_image_path, 90);

Должен быть

imagepng($thumbnail_gd_image, $thumbnail_image_path, 9);

У тебя, наверное, получилось. Но я бы порекомендовал php-библиотеку imagemagick. Если у вас есть доступ к нему на вашем веб-хостинге, это очень хорошая библиотека для работы с изображениями. Вы можете загрузить библиотеку, чтобы использовать ее локально.

0
Medda86 19 Мар 2014 в 09:41