php gd script not saving image properly - php

I've got this script, which only saves the image at $image, and not the image at $newimage_2. Help?
<?php
$newimage_1 = imagecopymerge($image, $watermark, $dest_x, $dest_y, 0, 0, $watermark_width, $watermark_height, $opacity);
$newimage_2 = imagecreatefromjpeg($newimage_1);
// print image to screen
header("content-type: image/jpeg");
imagejpeg($image);
imagejpeg($newimage_2);
imagedestroy($image);
imagedestroy($watermark);
imagedestroy($newimage_2);
?>

$source_file_path=$_FILES["image"]["tmp_name"];
$src = imagecreatefromjpeg($source_file_path);
list($width,$height)=getimagesize($source_file_path);
$newwidth=540;
$newheight=round(($height/$width)*$newwidth);
$tmp=imagecreatetruecolor($newwidth,$newheight);
imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight,$width,$height);
$target_file_path = "images/".$filewhereyouwanttosaveit;
$watermark = imagecreatefrompng('imgs/copyright.png');
$watermarkwidth = imagesx($watermark);
$watermarkheight = imagesy($watermark);
$startwidth = ($newwidth - $watermarkwidth);
$startheight = ($newheight - $watermarkheight);
imagecopy($tmp,$watermark,$startwidth,$startheight,0,0,$watermarkwidth,$watermarkheight);
imagegif($tmp,$target_file_path);
you probably dont need the resizing but code may help you...
imagegif or jpg or png or some else

Related

PHP image script not working

i have script for gallery. He takes image and shows it on that image url. But its not working, images wont show up on gallery pages. Everything else works. Database is okay, so are files on FTP. Any idea? Here is code of that scrip (config.php is okay too)
<?
require ('config.php');
$img = $_GET['img'];
if (!isset($img) or (!is_numeric($img)))
{ $masterURL = 'images/no_image.jpg'; }
else
{
$fotosql = MySQL_Query ("SELECT photo FROM photo WHERE id = '$img' LIMIT
1");
$foto_file = MySQL_Result ($fotosql, 0, 'photo');
$masterURL = 'photos/$foto_file';
}
header('content-type: image/png');
$watermark = imagecreatefrompng('images/watermark.png');
$watermark_width = imagesx($watermark);
$watermark_height = imagesy($watermark);
$image = imagecreatetruecolor($watermark_width, $watermark_height);
$image = imagecreatefromjpeg($masterURL);
$size = getimagesize($masterURL);
$dest_x = $size[0] - $watermark_width - 5;
$dest_y = $size[1] - $watermark_height - 5;
imagecopymerge($image, $watermark, $dest_x, $dest_y, 0, 0,
$watermark_width, $watermark_height, 100);
imagepng($image);
imagedestroy($image);
imagedestroy($watermark);
?>

PHP-GD Transparency of watermark PNG not correctly merged with JPEG

I am trying to install a watermark in the middle of my image, but every time it shows a weird square which is not fully transparent. This is the result of my code:
This is my code:
<?php
header("Content-type: image/png");
$image = imagecreatefromjpeg('http://www.sideshowtoy.com/wp-content/uploads/2016/03/dc-comics-batman-v-superman-woner-woman-sixth-scale-hot-toys-feature-902687.jpg');
$watermark = imagecreatefrompng('https://d5odq6jbm6umf.cloudfront.net/assets/img/video-play-button-transparent.png');
imagesavealpha($watermark,true);
$watermark_width = imagesx($watermark);
$watermark_height = imagesy($watermark);
$dest_x = (imagesx($image) - $watermark_width)/2;
$dest_y = (imagesy($image) - $watermark_height)/2;
imagecopymerge($image, $watermark, $dest_x, $dest_y, 0, 0, $watermark_width, $watermark_height, 100);
imagejpeg($image);
imagedestroy($image);
imagedestroy($watermark);
?>
I usually create a new true colour resource and copy everything into it. This ensures GD doesn't get too quirky. It's a little bit more resource intensive, but should be negligible for most cases.
Below is your code modified to create a new image, copy in the jpeg, and then overlay the partially transparent watermark:
<?php
$image = imagecreatefromjpeg('http://www.sideshowtoy.com/wp-content/uploads/2016/03/dc-comics-batman-v-superman-woner-woman-sixth-scale-hot-toys-feature-902687.jpg');
$img_w = imagesx($image);
$img_h = imagesy($image);
$new = imagecreatetruecolor($img_w, $img_h);
imagecopy($new, $image, 0, 0, 0, 0, $img_w, $img_h);
imagedestroy($image);
$watermark = imagecreatefrompng('https://d5odq6jbm6umf.cloudfront.net/assets/img/video-play-button-transparent.png');
$watermark_width = imagesx($watermark);
$watermark_height = imagesy($watermark);
$dest_x = ($img_w - $watermark_width) / 2;
$dest_y = ($img_h - $watermark_height) / 2;
imagecopy($new, $watermark, $dest_x, $dest_y, 0, 0, $watermark_width, $watermark_height);
header('Content-type: image/png');
imagejpeg($new);
imagedestroy($new);
imagedestroy($watermark);
Result:

PNG Watermark image on GIF does not appear

Adding water mark to jpg/jpeg and png works perfectly file but strange color appear when the same watermark is added to the gif image
the gif image with watermark is here
the strange color here is my png watermark image which is shown like this
my code
header('content-type: image/png');
$image = imagecreatefromgif($pic_url);
$imageSize = getimagesize($pic_url);
$watermark = imagecreatefrompng('../template/images/logo-watermark2.png');
$watermark_o_width = imagesx($watermark);
$watermark_o_height = imagesy($watermark);
$newWatermarkWidth = ($imageSize[0]-20)/2;
$newWatermarkHeight = $watermark_o_height * $newWatermarkWidth / $watermark_o_width;
imagecopyresized($image, $watermark, $imageSize[0]/2 - $newWatermarkWidth/2, $imageSize[1]/2 - $newWatermarkHeight/2, 0, 0, $newWatermarkWidth, $newWatermarkHeight, imagesx($watermark), imagesy($watermark));
imagegif($image);
imagedestroy($image);
imagedestroy($watermark);
Solved this after converting the gif image to a true color image
header('content-type: image/png');
$image = imagecreatefromgif($pic_url);
$imageSize = getimagesize($pic_url);
$watermark = imagecreatefrompng('../template/images/logo-watermark2.png');
$watermark_o_width = imagesx($watermark);
$watermark_o_height = imagesy($watermark);
$newWatermarkWidth = ($imageSize[0]-20)/2;
$newWatermarkHeight = $watermark_o_height * $newWatermarkWidth / $watermark_o_width;
$tmp = imagecreatetruecolor(imagesx($image), imagesy($image));
$bg = imagecolorallocate($tmp, 255, 255, 255);
imagefill($tmp, 0, 0, $bg);
imagecopy($tmp, $image, 0, 0, 0, 0, imagesx($image), imagesy($image));
$image = $tmp;
imagecopyresized($image, $watermark, $imageSize[0]/2 - $newWatermarkWidth/2, $imageSize[1]/2 - $newWatermarkHeight/2, 0, 0, $newWatermarkWidth, $newWatermarkHeight, imagesx($watermark), imagesy($watermark));
imagegif($image);
imagedestroy($image);
imagedestroy($watermark);

PHP imagecopy & imagecopymerge changing background from transparent to black

I am copying one image and placing over second image, second image is transparent in background. While copying background of second image is converted to black. Even if i display image just after initializing image from file, it gives me black background. Please help..
<?php
header('Content-type:image/png');
$watermark = imagecreatefrompng('eye.png');
$watermark_width = imagesx($watermark);
$watermark_height = imagesy($watermark);
$image = imagecreatetruecolor($watermark_width, $watermark_height);
$image = imagecreatefrompng('img.png');
$size = getimagesize('img.png');
$dest_x = $size[0] - $watermark_width - 5;
$dest_y = $size[1] - $watermark_height - 5;
imagecopyresampled($image, $watermark, 5, 5, 0,0,55 ,55, $watermark_width, $watermark_height);
//imagecopymerge($image, $watermark, 5, 5, 0, 0, $watermark_width, $watermark_height, 50);
imagejpeg($image);
imagedestroy($image);
imagedestroy($watermark);
?>
remove the function
imagejpeg($image);
and add
imagealphablending($image, false);
imagesavealpha($image,true);
imagepng($image);

image ontop of an image in php

Here is a link to the page http://www.true-gamerz.net/test2.php
if you look at the image, The rank image is transpent but it dose not show it on the screen
dose anyone know why this is happen?
Here is the code
<?
header('content-type: image/png');
$watermark = imagecreatefrompng('images/ranks/ranks_18.png');
$watermark_width = imagesx($watermark);
$watermark_height = imagesy($watermark);
$image = imagecreatetruecolor($watermark_width, $watermark_height);
$image = imagecreatefrompng("images/card/test.png");
$size = getimagesize("images/card/test.png");
$dest_x = $watermark_width;
$dest_y = $watermark_height;
imagecopymerge($image, $watermark, 289, 4, 0, 0, $watermark_width, $watermark_height, 100);
imagepng($image);
imagedestroy($image);
imagedestroy($watermark);
?>
imagecopymerge does not support alpha channel
Read this for workaround:
http://www.php.net/manual/en/function.imagecopymerge.php#92787

Categories