PHP image script not working - php

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);
?>

Related

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:

imagecopymerge() For two images Already in Server (watermark usage)

I have two photos already in server, trying to use one for WATERMARK..
I am using the below script:
<?php
$watermark = "../images/watermark/watermark.gif";
$image = "../images/mainphoto.gif";
$padding = 0;
$opacity = 100;
$watermark_size = getimagesize($watermark);
$watermark_width = $watermark_size[0];
$watermark_height = $watermark_size[1];
$image_size = getimagesize($image);
$dest_x = $image_size[0] - $watermark_width - $padding;
$dest_y = $image_size[1] - $watermark_height - $padding;
if(!$image || !$watermark) die("Error: main image or watermark could not be loaded!");
imagecopymerge($image, $watermark, $dest_x, $dest_y, 0, 0, $watermark_width, $watermark_height, $opacity);
?>
I am getting the following error:
Warning: imagecopymerge() expects parameter 1 to be resource, string given in /home/neatbuz/public_html/asite_service/inc_watermark.php on line 26
please help..
$image = imagecreatefromgif ( "../images/mainphoto.gif" );
$watermark = imagecreatefromgif ( "../images/watermark/watermark.gif" );
i'd recommend to use png instead of gif images

Unable to water mark images with php

I was unable to watermark my images. I used the php manual found at this link
http://www.php.net/manual/en/image.examples-watermark.php
And also tried the sitepoint tutorial here
http://www.sitepoint.com/watermark-images-php/
but getting the same error that the image cannot be displayed because its have some errors. can somebody let me know whats wrong with the code or suggest me some better solution.
My code is here :
header('content-type: image/jpeg');
$watermark = imagecreatefrompng('wm.png');
$watermark_width = imagesx($watermark);
$watermark_height = imagesy($watermark);
$image = imagecreatetruecolor($watermark_width, $watermark_height);
$image = imagecreatefromjpeg('img.jpg');
$size = getimagesize('img.jpg');
$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);
imagejpeg($image);
imagedestroy($image);
imagedestroy($watermark);
Check all the return values of the functions you call, e.g.
$watermark = imagecreatefrompng('wm.png');
if ( !$watermark ) {
die('Sorry ' . __LINE__); // you might want to use something else here - just an example....
}
and then set the content type to image/jpeg not before you're actually trying to send the image
if ( headers_sent($file, $line) ) {
die('oops '.__LINE__);
}
else {
header('content-type: image/jpeg');
imagejpeg($image);
}
...makes it easier to pin-point the error.

php gd script not saving image properly

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

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