Adapt transparency code in this PHP code

1

Hello, I want to put this code below in my current one that does image resizing via link ( <img src='resize.php?w=100&h=100&img=img.jpg'> ).

$newImg = imagecreatetruecolor($nWidth, $nHeight);
/* Checando se a imagem é PNG ou GIF, então seta como transparent*/  
    if(($imgInfo[2] == 1) OR ($imgInfo[2]==3)){
    imagealphablending($newImg, false);
    imagesavealpha($newImg,true);
    $transparent = imagecolorallocatealpha($newImg, 255, 255, 255, 127);
    imagefilledrectangle($newImg, 0, 0, $nWidth, $nHeight, $transparent);
    }
    imagecopyresampled($newImg, $im, 0, 0, 0, 0, $nWidth, $nHeight, $imgInfo[0], $imgInfo[1]);

The code above is original from where I want to get it. To see complete: CLICK HERE

My code:

<?php
session_start();
header("Pragma: public");
header("Cache-Control: max-age = 604800");
header("Expires: ".gmdate("D, d M Y H:i:s", time() + 604800)." GMT");

function thumbnail($image, $width, $height) {

    if($image[0] != "/") { // Decide where to look for the image if a full path is not given
        if(!isset($_SERVER["HTTP_REFERER"])) { // Try to find image if accessed directly from this script in a browser
            $image = $_SERVER["DOCUMENT_ROOT"].implode("/", (explode('/', $_SERVER["PHP_SELF"], -1)))."/".$image;
        } else {
            $image = implode("/", (explode('/', $_SERVER["HTTP_REFERER"], -1)))."/".$image;
        }
    } else {
        $image = $_SERVER["DOCUMENT_ROOT"].$image;
    }
    $image_properties = @getimagesize($image);
    $image_width = $image_properties[0];
    $image_height = $image_properties[1];
    @$image_ratio = ($image_width/$image_height);
    $type = $image_properties["mime"];

    if(!$width && !$height) {
        $width = $image_width;
        $height = $image_height;
    }
    if(!$width) {
        $width = round($height * $image_ratio);
    }
    if(!$height) {
        $height = round($width / $image_ratio);
    }

    if($type == "image/jpeg") {
        header('Content-type: image/jpeg');
        $thumb = @imagecreatefromjpeg($image);
    } elseif($type == "image/png") {
        header('Content-type: image/png');
        $thumb = @imagecreatefrompng($image);
    }
    elseif($type == "image/gif") {
        header('Content-type: image/gif');
        $thumb = @imagecreatefromgif($image);
    } else {
        return false;
    }

    $temp_image = imagecreatetruecolor($width, $height);
    imagecopyresampled($temp_image, $thumb, 0, 0, 0, 0, $width, $height, $image_width, $image_height);
    $thumbnail = imagecreatetruecolor($width, $height);
    imagecopyresampled($thumbnail, $temp_image, 0, 0, 0, 0, $width, $height, $width, $height);

    if($type == "image/jpeg") {
        imagejpeg($thumbnail);
    }
    else if($type == "image/gif") {
        imagegif($thumbnail);
    }
    else {
        imagepng($thumbnail);
    }

    imagedestroy($temp_image);
    imagedestroy($thumbnail);

}

if(isset($_GET["h"])) { $h = $_GET["h"]; } else { $h = 0; }
if(isset($_GET["w"])) { $w = $_GET["w"]; } else { $w = 0; }

thumbnail($_GET["img"], $w, $h);

?>
  

The problem is that I do not know what I'm doing wrong, because the image is all black.

Can somebody with experience help me adapt?

    
asked by anonymous 17.12.2017 / 08:40

1 answer

1

To use the alpha channel with the imagecreatetruecolor function, you need to use two other functions.

imagealphablending and imagesavealpha .

To do this simply replace:

$temp_image = imagecreatetruecolor($width, $height);
imagecopyresampled($temp_image, $thumb, 0, 0, 0, 0, $width, $height, $image_width, $image_height);
$thumbnail = imagecreatetruecolor($width, $height);
imagecopyresampled($thumbnail, $temp_image, 0, 0, 0, 0, $width, $height, $width, $height);

By:

$thumbnail = imagecreatetruecolor($width, $height);
imagealphablending($thumbnail, false);
imagesavealpha($thumbnail, true);
imagecopyresampled($thumbnail, $thumb, 0, 0, 0, 0, $width, $height, $image_width, $image_height);
    
17.12.2017 / 09:42