使用PHP创建PNG缩略图

时间:2014-07-08 02:51:43

标签: php image png gd php-gd

我正在尝试创建一个缩略图,它返回一个错误,imagecopyresized()期望2个参数是资源,但它可以创建图像,但输出只是一个小的黑色图像。

这是我的代码

$image = "asd.PNG";

$image_size = getimagesize($image);
$image_width = $image_size[0]; 
$image_height = $image_size[1];


$new_size = ($image_width + $image_height)/($image_width*($image_height/45));

$new_width = $image_width * $new_size;

$new_height = $image_height * $new_size;

$new_image = imagecreatetruecolor($new_width, $new_height);

$old_mage = imagecreatefrompng($image); 

imagecopyresized($new_image, $image, 0, 0, 0, 0, $new_width, $new_height, $image_width, $image_height);

imagepng($new_image, $image.'.thumb.png');

4 个答案:

答案 0 :(得分:1)

这是一个功能。使用辅助功能实现PNG透明度。

public function redimesionImage($endThu,$newX,$newY,$endImg,$fileType){

    // Copy the image to be resized
    copy($endThu, $endImg);

    // Retrieves the image data
    list($width, $height) = getimagesize($endImg);

    // If the width is greater ...
    if($width >= $height) {

        // I set the width of the image to the desired size ...
        $newXimage = $newX;

        // And calculate the size of the time to not stretch the image
        $newYimage = ($height / $width) * $newXimage;

    } else {

        // Define the desired height ...
        $newYimage = $newY;

        // And calculate the width to not stretch the image
        $newXimage = ($width / $height) * $newYimage;
    }

    // Creates an initial image in memory with calculated measures
    $imageInicial = imagecreatetruecolor(ceil($newXimage), ceil($newYimage));

    // I check the extension of the image and create their respective image
    if ($fileType == 'jpeg')   $endereco = imagecreatefromjpeg($endImg);
    if ($fileType == 'jpg')  $endereco = imagecreatefromjpeg($endImg);      
    if ($fileType == 'png')    {
        $endereco = imagecreatefrompng($endImg);
        imagealphablending($imageInicial, false);
        imagesavealpha($imageInicial,true);
        $transparent = imagecolorallocatealpha($imageInicial, 255, 255, 255, 127);
        imagefilledrectangle($endereco, 0, 0, $newXimage, $newYimage, $transparent);
    }
    if ($fileType == 'gif')  {
        $endereco = imagecreatefromgif($endImg);
        $this->setTransparency($imageInicial,$endereco);
    }

    // I merge the image to be resized with created in memory
    imagecopyresampled($imageInicial, $endereco, 0, 0, 0, 0, ceil($newXimage), ceil($newYimage), ceil($width), ceil($height));

    // Creates the image in its final lacal, according to its extension
    if ($fileType == 'jpeg') imagejpeg($imageInicial, $endImg, 100);
    if ($fileType == 'jpg') imagejpeg($imageInicial, $endImg, 100);
    if ($fileType == 'png') imagepng($imageInicial, $endImg, 9);
    if ($fileType == 'gif') imagegif($imageInicial, $endImg, 100);
}

// Function to assist the PNG images, sets the transparency in the image
private function setTransparency($new_image,$image_source){ 
    $transparencyIndex = imagecolortransparent($image_source); 
    $transparencyColor = array('red' => 255, 'green' => 255, 'blue' => 255);     
    if($transparencyIndex >= 0){
        $transparencyColor = imagecolorsforindex($image_source, $transparencyIndex);    
    }
    $transparencyIndex = imagecolorallocate($new_image, $transparencyColor['red'], $transparencyColor['green'], $transparencyColor['blue']); 
    imagefill($new_image, 0, 0, $transparencyIndex); 
    imagecolortransparent($new_image, $transparencyIndex);        
}

答案 1 :(得分:0)

public function redimesionImage($endThu,$newX,$newY,$endImg,$fileType){

    copy($endThu, $endImg);

    list($width, $height) = getimagesize($endImg);

    if($width >= $height) {

        $newXimage = $newX;

        $newYimage = ($height / $width) * $newXimage;

    } else {

        $newYimage = $newY;

        $newXimage = ($width / $height) * $newYimage;
    }

    $imageInicial = imagecreatetruecolor(ceil($newXimage), ceil($newYimage));

    if ($fileType == 'jpeg')   $endereco = imagecreatefromjpeg($endImg);
    if ($fileType == 'jpg')  $endereco = imagecreatefromjpeg($endImg);      
    if ($fileType == 'png')    {
        $endereco = imagecreatefrompng($endImg);
        imagealphablending($imageInicial, false);
        imagesavealpha($imageInicial,true);
        $transparent = imagecolorallocatealpha($imageInicial, 255, 255, 255, 127);
        imagefilledrectangle($endereco, 0, 0, $newXimage, $newYimage, $transparent);
    }
    if ($fileType == 'gif')  {
        $endereco = imagecreatefromgif($endImg);
        $this->setTransparency($imageInicial,$endereco);
    }

    imagecopyresampled($imageInicial, $endereco, 0, 0, 0, 0, ceil($newXimage), ceil($newYimage), ceil($width), ceil($height));

    if ($fileType == 'jpeg') imagejpeg($imageInicial, $endImg, 100);
    if ($fileType == 'jpg') imagejpeg($imageInicial, $endImg, 100);
    if ($fileType == 'png') imagepng($imageInicial, $endImg, 9);
    if ($fileType == 'gif') imagegif($imageInicial, $endImg, 100);
}
private function setTransparency($new_image,$image_source){ 
    $transparencyIndex = imagecolortransparent($image_source); 
    $transparencyColor = array('red' => 255, 'green' => 255, 'blue' => 255);     
    if($transparencyIndex >= 0){
        $transparencyColor = imagecolorsforindex($image_source, $transparencyIndex);    
    }
    $transparencyIndex = imagecolorallocate($new_image, $transparencyColor['red'], $transparencyColor['green'], $transparencyColor['blue']); 
    imagefill($new_image, 0, 0, $transparencyIndex); 
    imagecolortransparent($new_image, $transparencyIndex);        
}

答案 2 :(得分:0)

您使用imagecreatefrompng()加载图片,因此您必须确保图片格式为png,而不是jpeg或其他格式。你可以使用测试用函数imagecreatefromjpeg()

答案 3 :(得分:0)

以下是创建缩略图的两种不同方法。

第一种方式

function createThumbnail($filepath, $thumbpath, $thumbnail_width, $thumbnail_height, $background=false) {
        list($original_width, $original_height, $original_type) = getimagesize($filepath);
        if ($original_width > $original_height) {
            $new_width = $thumbnail_width;
            $new_height = intval($original_height * $new_width / $original_width);
        } else {
            $new_height = $thumbnail_height;
            $new_width = intval($original_width * $new_height / $original_height);
        }
        $dest_x = intval(($thumbnail_width - $new_width) / 2);
        $dest_y = intval(($thumbnail_height - $new_height) / 2);

        if ($original_type === 1) {
            $imgt = "ImageGIF";
            $imgcreatefrom = "ImageCreateFromGIF";
        } else if ($original_type === 2) {
            $imgt = "ImageJPEG";
            $imgcreatefrom = "ImageCreateFromJPEG";
        } else if ($original_type === 3) {
            $imgt = "ImagePNG";
            $imgcreatefrom = "ImageCreateFromPNG";
        } else {
            return false;
        }

        $old_image = $imgcreatefrom($filepath);
        $new_image = imagecreatetruecolor($thumbnail_width, $thumbnail_height); // creates new image, but with a black background

        // figuring out the color for the background
        if($original_type == 1 || $original_type == 2) {
          list($red, $green, $blue) = $background;
          $color = imagecolorallocate($new_image, $red, $green, $blue);
          imagefill($new_image, 0, 0, $color);
        // apply transparent background only if is a png image
        } else if($original_type == 3){
            imagealphablending($new_image, false);
            imagesavealpha($new_image, true);
          //imagesavealpha($new_image, TRUE);
          //$color = imagecolorallocatealpha($new_image, 0, 0, 0, 127);
          //imagefill($new_image, 0, 0, $color);
        }

        imagecopyresampled($new_image, $old_image, $dest_x, $dest_y, 0, 0, $new_width, $new_height, $original_width, $original_height);
        $imgt($new_image, $thumbpath);
        return file_exists($thumbpath);
}

另一种方式:

function createThumbnails($filepath, $thumbpath, $thumbnail_height, $background=false) {
        list($width, $height, $original_type) = getimagesize($filepath);
    
        
        $new_width = floor( $width * ( $thumbnail_height / $height ) );
        $new_height = $thumbnail_height;

        if ($original_type === 1) {
            $imgt = "ImageGIF";
            $imgcreatefrom = "ImageCreateFromGIF";
        } else if ($original_type === 2) {
            $imgt = "ImageJPEG";
            $imgcreatefrom = "ImageCreateFromJPEG";
        } else if ($original_type === 3) {
            $imgt = "ImagePNG";
            $imgcreatefrom = "ImageCreateFromPNG";
        } else {
            return false;
        }

        $old_image = $imgcreatefrom($filepath);
        $new_image = imagecreatetruecolor($new_width, $new_height); // creates new image, but with a black background

        // figuring out the color for the background
        if($original_type == 1 || $original_type == 2) {
          list($red, $green, $blue) = $background;
          $color = imagecolorallocate($new_image, $red, $green, $blue);
          imagefill($new_image, 0, 0, $color);
        // apply transparent background only if is a png image
        } else if($original_type == 3){
            imagealphablending($new_image, false);
            imagesavealpha($new_image, true);
          //imagesavealpha($new_image, TRUE);
          //$color = imagecolorallocatealpha($new_image, 0, 0, 0, 127);
          //imagefill($new_image, 0, 0, $color);
        }

        imagecopyresampled($new_image, $old_image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
        $imgt($new_image, $thumbpath);
        return file_exists($thumbpath);
}