0

i have the following script for coping a JPEG and a PNG to an existing PNG called base.png. Within the function "transparent_background" i replace the white background with transparency. This function is the problem. Standlone the function is working with output directly in the browser. please see the comment out "//imagepng($img);". but if i return the $img out of the function its still a jpeg i think, thats why it isnt transparent. The second function is just for resize.

<?php

function transparent_background($img) 
{
    $img = imagecreatefromjpeg($img); //or whatever loading function you need
    $colors= array("255","255","255");
    $remove = imagecolorallocate($img, $colors[0], $colors[1], $colors[2]);
    imagecolortransparent($img, $remove);
    //imagepng($img);
    return $img;
    imagedestroy($img);
}
function resize($img, $w){
    $img = imagecreatefromjpeg($img);
    $ratio = imagesx($img)/imagesy($img); 
    if( $ratio > 1) {
        $width = $w;
        $height = $w/$ratio;
    }
    else {
        $width = $w*$ratio;
        $height = $w;
    }
    $dst = imagecreatetruecolor($width,$height);
    imagecopyresampled($dst,$img,0,0,0,0,$width,$height,imagesx($img),imagesy($img));
    return $dst;
    imagedestroy($dst);
    imagedestroy($img);
}

$h="https://images-eu.ssl-images-amazon.com/images/I/415zYwg2-TL.jpg";
$base = imagecreatefrompng("base.png");
$logo = imagecreatefrompng("fs_logo_line.png");

$pos1=resize($h,"730");
$pos1=transparent_background($h);
imagecopy($base,$pos1,0, 5, 0, 0, imagesx($pos1),imagesy($pos1));
imagecopy($base,$logo,0, 1136, 0,0,imagesx($logo),imagesy($logo));

imagepng($base);

?>

I think the problem is, that i get a jpeg back from the transparent_background function and thats why the image in $pos1 is not transparent. Any ideas how i can solve that? I have tried with ob_start & ob_get_contents but this also didn't work.

swapfile
  • 415
  • 2
  • 19

1 Answers1

0

You can merge the two images together using the PHP GD2 library.

Example:

<?php
 # If you don't know the type of image you are using as your originals.
 $image = imagecreatefromstring(file_get_contents($your_original_image));
 $frame = imagecreatefromstring(file_get_contents($your_frame_image));

 # If you know your originals are of type PNG.
 $image = imagecreatefrompng($your_original_image);
 $frame = imagecreatefrompng($your_frame_image);

 imagecopymerge($image, $frame, 0, 0, 0, 0, 50, 50, 100);

 # Save the image to a file
 imagepng($image, '/path/to/save/image.png');

 # Output straight to the browser.
 imagepng($image);
?>

Add imagealphablending($frame,true); before imagecopymerge() if you want to keep PNG frame transparancy over the image.

Vishwas Soni
  • 467
  • 10
  • 16
  • Thanks, @Vishwas Soni. I'll try that. But i think the Problem is within the function transparent_background. I push a jpeg to the function doing the transparency thing and do not output the file to the browser/file with imagepng. so i think the problem is that i return a jpeg from the function. so is there a way to convert a jpeg to png without output to browser or file. Just within a variable. – swapfile May 17 '17 at 06:04
  • @swapfile This answer might help you. http://stackoverflow.com/questions/21105802/convert-image-format-png-to-jpeg-without-saving-to-disk-php – Vishwas Soni May 17 '17 at 06:18
  • thanks for the link. but here's also a output to the browser which i don't want. i want to have the png within the return variable. and return imagepng($image); doesn't work. – swapfile May 17 '17 at 06:22