简体   繁体   English

除了使用imagejpeg或imagepng之外,还有更高质量的将PHP图像资源转换为jpeg或png的方法吗?

[英]Is there higher quality way of converting php image resources to jpeg or png besides using imagejpeg or imagepng

转换为PHP图像资源之前的图像

转换为php资源后的图像和使用imagepng的png

Currently I am using imagepng to convert an image resource based off of an original image to an png image type. 目前我使用imagepng将基于原始图像的图像资源转换为png图像类型。 This proved to have a higher level of quality than the imagejpeg function was creating for my jpg. 这被证明比imagejpeg函数为我的jpg创建的质量更高。

However you can clearly see that the compression algorithms in these two functions are not of as high quality as one that would be in something like Photoshop. 但是你可以清楚地看到这两个函数中的压缩算法的质量不如Photoshop那样高。

Does anyone know of a way to get a higher quality jpeg? 有谁知道如何获得更高质量的jpeg? Is there documentation on the format of the image resource and how it is converted to a jpeg or png? 是否有关于图像资源格式以及如何将其转换为jpeg或png的文档? If I had that information I could at least try to implement a better algorithm. 如果我有这些信息,我至少可以尝试实现更好的算法。

//Create image
        $im = apppic($filename, $colors);
        imagepng($im, "images/app/".rtrim($path_array[$count],"jpeg")."png", 0);

function promopic ($filename, $colors){


    if(@imagecreatefromjpeg($filename))
            $img = imagecreatefromjpeg($filename);
    elseif(@imagecreatefrompng($filename))
            $img = imagecreatefrompng($filename);
    elseif(@imagecreatefromgif($filename))
            $img = imagecreatefromgif($filename);
    $width = imagesx($img);
    $height = imagesy($img);
    $imHeight = 625;

    if( $width/$height > 4/5){$imWidth = 800; $imHeight = $height/$width*$imWidth ; }
    else {  $imWidth = $width/$height*$imHeight;  }

    $colorWidth = 1200 - $imWidth;
    $numColors = count($colors);
    $colorHeight = ceil($imHeight/$numColors) - 2;
    $imHeight = ceil($imHeight/$numColors)* $numColors - 2;


    $im = @imagecreate(1200, $imHeight+50)
        or die("Cannot Initialize new GD image stream");
    $background_color = imagecolorallocate($im, 250, 250, 250);
    $text_color = imagecolorallocate($im, 255, 255, 251);
    $blue_color = imagecolorallocate($im, 40, 5, 251);


    //Add color boxes
    for ($i = 1; $i <= $numColors; $i++) {
        $imcolorbox = @imagecreate($colorWidth, $colorHeight);
        $rgb = hex2rgb($colors[($i-1)]);

        $colorbox_color = imagecolorallocate($imcolorbox, $rgb[0], $rgb[1], $rgb[2]);
        $dest_x = 1200-$colorWidth;
        $dest_y = ($colorHeight + 2) * ($i-1);

        $copy_image = imagecopy($im, $imcolorbox,  $dest_x, $dest_y, 0, 0, $colorWidth, $colorHeight);
        imagedestroy($imcolorbox);
        //imagestring($im, 5, 1075, 2+$dest_y,  "Reinvogue.com", $text_color);
        //imagestring($im, 5, $imWidth+5, 2+$dest_y,  "Reinvogue.com", $text_color);
        //imagestring($im, 5, 1050, 17+$dest_y,  "Hex: ".$colors[($i-1)], $text_color);
        //imagestring($im, 5, 1050, 2+$dest_y,  "RGB: ".$rgb[0]." ".$rgb[1]." ".$rgb[2], $text_color);
    }

     imagestring($im, 5, 10, $imHeight+15,  "Powered by the Reinvogue.com Colorway Engine", $blue_color);


    $copy_image = imagecopyresampled($im, $img,  0, 0, 0, 0, $imWidth-2, $imHeight, $width, $height);
    return $im;
}

This should create a high quality image with a white background: 这应该创建一个白色背景的高品质图像:

$im = @imagecreatetruecolor(1200, $imHeight+50)
$white = imagecolorallocate($im, 255, 255, 255);
imagefill($im, 0, 0, $white);

If you're working with png images you can even have a look at imagecolorallocatealpha ( http://www.php.net/manual/en/function.imagecolorallocatealpha.php ) 如果您正在使用png图像,您甚至可以查看imagecolorallocatealphahttp://www.php.net/manual/en/function.imagecolorallocatealpha.php

Hope this helps. 希望这可以帮助。

For high-quality PNG8 use pngquant2 . 对于高质量的PNG8,请使用pngquant2 PHP's built-in libgd fork has awful palette conversion. PHP的内置libgd fork具有糟糕的调色板转换。

Quoting: http://pngquant.org/php.html 引用: http//pngquant.org/php.html

<?php

/**
 * Optimizes PNG file with pngquant 1.8 or later (reduces file size of 24-bit/32-bit PNG images).
 *
 * You need to install pngquant 1.8 on the server (ancient version 1.0 won't work).
 * There's package for Debian/Ubuntu and RPM for other distributions on http://pngquant.org
 *
 * @param $path_to_png_file string - path to any PNG file, e.g. $_FILE['file']['tmp_name']
 * @param $max_quality int - conversion quality, useful values from 60 to 100 (smaller number = smaller file)
 * @return string - content of PNG file after conversion
 */
function compress_png($path_to_png_file, $max_quality = 90)
{
    if (!file_exists($path_to_png_file)) {
        throw new Exception("File does not exist: $path_to_png_file");
    }

    // guarantee that quality won't be worse than that.
    $min_quality = 60;

    // '-' makes it use stdout, required to save to $compressed_png_content variable
    // '<' makes it read from the given file path
    // escapeshellarg() makes this safe to use with any path
    $compressed_png_content = shell_exec("pngquant --quality=$min_quality-$max_quality - < ".escapeshellarg(    $path_to_png_file));

    if (!$compressed_png_content) {
        throw new Exception("Conversion to compressed PNG failed. Is pngquant 1.8+ installed on the server?");
    }

    return $compressed_png_content;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM