merge image by gd in php - php

I want to merge two images by gd library in php
first image is my local image which is in the same folder of my file
but i want to get the second image from somewhere else in internet
i mean , the second image is something like this : http://www.somewhere.com/pics/image.jpg
i try to merge but it doesnt work :(
this is my code:
source is my jpeg URL. it works when jpeg file is in the same folder !!
header('Content-Type: image/jpeg');
if ( isset( $_POST['source'] ) ){
$source = $_POST['source'];
$watermark = imagecreatefrompng( 'logo.png' );
$watermark_width = imagesx( $watermark );
$watermark_height = imagesy( $watermark );
$image = imagecreatetruecolor( $watermark_width, $watermark_height );
$image = imagecreatefromjpeg( $source );
$imagesize = getimagesize( $source );
$x = $imagesize[0] - $watermark_width - 10;
$y = $imagesize[1] - $watermark_height - 10;
imagecopymerge( $image, $watermark, $x, $y, 0, 0, $watermark_width, $watermark_height, 20 );
imagejpeg( $image );
}
Thank you.

There are a couple ways to solve this. You can use curl to pull the remote image down to your local machine. Since you said your code works when the image is in the same folder, you should be all set then.

Related

Php Image upload failing and using imagecreatefromjpeg to process png and bmp files?

Hello I have the following code to create a thumbnail of an image then upload it. The code is failing when I try to upload the image attached below and I don't know why. Previous to this code there is a main image upload which always works but the thumbnail script above works most of the time but not with the image attached for some reason. The code dies so the page outputs the success of the main image upload but the thumbnail never uploads and neither does the rest of the page.
Also will this code process images other than jpeg? If it won't how would I go about making it process other file types like bmp and png?
// load image and get image size
$img = imagecreatefromjpeg( $upload_path . $filename );
$width = imagesx( $img );
$height = imagesy( $img );
// calculate thumbnail size
$new_height = $thumbHeight;
$new_width = floor( $width * ( $thumbHeight / $height ) );
// create a new temporary image
$tmp_img = imagecreatetruecolor( $new_width, $new_height );
// copy and resize old image into new image
imagecopyresized( $tmp_img, $img, 0, 0, 0, 0, $new_width, $new_height, $width, $height );
// save thumbnail into a file
imagejpeg( $tmp_img, $upload_paththumbs . $filename );
Image that fails
Your code works for me with your image, so the problem must be with the set values of your input variables.
As already suggested in the comments, check your PHP error log. If that doesn't show anything abnormal you'll need debug the code line-by-line.
For the second part of your question: no, your code won't work for images other than JPEG. The changes, below, will handle GIF, JPEG, and PNG.
Note that the function exif_imagetype() might not be available by default. In this case you'll need to activate the exif extension in your PHP config.
$upload_path = './';
$filename = 'b4bAx.jpg';
$upload_paththumbs = './thumb-';
$thumbHeight = 50;
switch ( exif_imagetype( $upload_path . $filename ) ) {
case IMAGETYPE_GIF:
imagegif(
thumb(imagecreatefromgif( $upload_path . $filename ), $thumbHeight),
$upload_paththumbs . $filename
);
break;
case IMAGETYPE_JPEG:
imagejpeg(
thumb(imagecreatefromjpeg( $upload_path . $filename ), $thumbHeight),
$upload_paththumbs . $filename
);
break;
case IMAGETYPE_PNG:
imagepng(
thumb(imagecreatefrompng( $upload_path . $filename ), $thumbHeight),
$upload_paththumbs . $filename
);
break;
default:
echo 'Unsupported image type!';
}
function thumb($img, $thumbHeight) {
// get image size
$width = imagesx( $img );
$height = imagesy( $img );
// calculate thumbnail size
$new_height = $thumbHeight;
$new_width = floor( $width * ( $thumbHeight / $height ) );
// create a new temporary image
$tmp_img = imagecreatetruecolor( $new_width, $new_height );
// copy and resize old image into new image
imagecopyresampled( $tmp_img, $img, 0, 0, 0, 0, $new_width, $new_height, $width, $height );
// return thumbnail
return $tmp_img;
}

issue when trying to copy image from remote location using php

I'm trying to copy an image from a remote location. I created a function that takes in the file name ($fname) and the path of the image ($remote_path). Currently I'm running PHP 5.3.28 with the following code:
function copy_remote_image($fname, $remote_path){
$thumbWidth = 612;
$folder = 'uploads/photos/';
$img = imagecreatefromjpeg( $remote_path );
if($img){
$width = imagesx( $img );
$height = imagesy( $img );
// calculate thumbnail size
$new_width = $thumbWidth;
$new_height = floor( $height * ( $thumbWidth / $width ) );
// create a new temporary image
$tmp_img = imagecreatetruecolor( $new_width, $new_height );
// copy and resize old image into new image
imagecopyresampled( $tmp_img, $img, 0, 0, 0, 0, $new_width, $new_height, $width, $height );
// save thumbnail into a file
imagejpeg( $tmp_img, $folder.$fname,70 );
imagedestroy($tmp_img);
return true;
}else{
return false;
}
}
As of now the image is not copying and when I use var_dump on $img i get: resource(25) of type (gd)

PHP: Watermark won't work

I've created a watermark function as below :
index.php
<?php
include 'core.inc.php';
do_watermark('bg1.jpg', 'logo1.png');
?>
core.inc.php
<?php
header ( 'Content-type: image/jpeg' );
function do_watermark($source_image, $logo) {
$watermark = imagecreatefrompng ( $logo );
$watermark_width = imagesx ( $logo );
$watermark_height = imagesy ( $logo );
$image = imagecreatetruecolor ( $watermark_width, $watermark_height );
$image = imagecreatefromjpeg ( $source_image );
$image_size = getimagesize ( $source_image );
$x = $image_size [0] - $watermark_width - 10;
$y = $image_size [1] - $watermark_height - 10;
imagecopymerge ( $image, $watermark, $x, $y, 0, 0, $watermark_width, $watermark_height, 40 );
imagejpeg ( $image );
}
?>
But when calling do_watermark('bg1.jpg', 'logo1.png'), show nothing.
bg1.jpg and logo1.png is along index.php.
Any help would be great.
Isn't the watermark image usually going to be smaller than the image being watermarked? You have created your working image to be the watermark's size rather than the source image's size -- is that what you really want? I'm not sure about the x and y coordinates -- the larger the watermark is, the further right and (up or down?) it will be, as x and y increase with the size of the watermark. I would think about positioning it (x and y) as a function of both the image size and the watermark size.
You also have it hard coded for JPEG image and PNG watermark. You could get the image types from the file names or from the getimagesize() call ('mime' entry). Consider making it more flexible this way.
There were some major problems in the code you had written, this should work, but compare it to yours to see what was wrong.
<?php
function do_watermark($source_image, $logo) {
$watermark = imagecreatefrompng($logo);
$watermark_width = imagesx($watermark);
$watermark_height = imagesy($watermark);
$image = imagecreatefromjpeg($source_image);
$image_width = imagesx($image);
$image_height = imagesy($image);
$x = $image_width - $watermark_width - 10;
$y = $image_height - $watermark_height - 10;
imagecopymerge($image, $watermark, $x, $y, 0, 0, $watermark_width, $watermark_height, 40 );
header('Content-Type: image/jpeg');
imagejpeg($image);
imagedestroy($image);
}
?>

PHP createThumbnail function troubleshooting

I'm using this function, to create thumbnails of images uploaded by the user, that I found here: http://webcheatsheet.com/php/create_thumbnail_images.php :
function createThumbs( $pathToImages, $pathToThumbs, $thumbWidth )
{
// open the directory
$dir = opendir( $pathToImages );
// loop through it, looking for any/all JPG files:
if (false !== ($fname = readdir( $dir ))) {
// parse path for the extension
$info = pathinfo($pathToImages . $fname);
// continue only if this is a JPEG image
if ( strtolower($info['extension']) == 'jpg' )
{
echo "Creating thumbnail for {$fname} <br />";
// load image and get image size
$img = imagecreatefromjpeg( "{$pathToImages}{$fname}" );
$width = imagesx( $img );
$height = imagesy( $img );
// calculate thumbnail size
$new_width = $thumbWidth;
$new_height = floor( $height * ( $thumbWidth / $width ) );
// create a new temporary image
$tmp_img = imagecreatetruecolor( $new_width, $new_height );
// copy and resize old image into new image
imagecopyresampled( $tmp_img, $img, 0, 0, 0, 0, $new_width, $new_height, $width, $height );
// save thumbnail into a file
imagejpeg( $tmp_img, "{$pathToThumbs}{$fname}" );
}
}
// close the directory
closedir( $dir );
}
This function works fine and does exactly what I want it to, but, despite this, I'm still getting errors from it. See errors below:
Warning: opendir(images/008/01/0000288988r.jpg,images/008/01/0000288988r.jpg) [<a href='function.opendir'>function.opendir</a>]: The directory name is invalid. (code: 267)
Warning: opendir(images/008/01/0000288988r.jpg) [<a href='function.opendir'>function.opendir</a>]: failed to open dir: No error
Warning: readdir() expects parameter 1 to be resource, boolean given
The problem, I think, is that I'm passing an actual file, not just a directory, into the parameters for the function. This is the case for $pathtoimages and $pathtothumbs. The function is supposed to search through the directory passed to it to find all images with the .jpg extension. But I would like to just perform the function on the one image uploaded at the time of upload. Is there someway to edit this function to allow for this?
Thanks in advance
the $pathToImage must point to image file
remove
$dir = opendir( $pathToImages );
if (false !== ($fname = readdir( $dir ))) {
// parse path for the extension
$info = pathinfo($pathToImages . $fname);
add $info = pathinfo($pathtoImages); // for the file name
$fname = $info['filename']
replace {$pathToImages}{$fname} with $pathToImages only, since its the image file.
btw, this code is not verbose.
function createThumbs( $pathToImages, $pathToThumbs, $thumbWidth )
{
// load image and get image size
$img = imagecreatefromjpeg( "{$pathToImages}" );
$width = imagesx( $img );
$height = imagesy( $img );
// calculate thumbnail size
$new_width = $thumbWidth;
$new_height = floor( $height * ( $thumbWidth / $width ) );
// create a new temporary image
$tmp_img = imagecreatetruecolor( $new_width, $new_height );
// copy and resize old image into new image
imagecopyresampled( $tmp_img, $img, 0, 0, 0, 0, $new_width, $new_height, $width, $height );
// save thumbnail into a file
imagejpeg( $tmp_img, "{$pathToThumbs}" );
}
Think I prematurely posted this question. Thanks for help from everyone.
#csw looks like your solution may have worked, but I got mine working too so I didn't test it.
Quick and dirty:
function createThumb( $pathToImage, $pathToThumb, $thumbWidth )
{
$fname = $pathToImage;
echo "Creating thumbnail for {$fname} <br />";
// load image and get image size
$img = imagecreatefromjpeg( "{$pathToImage}{$fname}" );
$width = imagesx( $img );
$height = imagesy( $img );
// calculate thumbnail size
$new_width = $thumbWidth;
$new_height = floor( $height * ( $thumbWidth / $width ) );
// create a new temporary image
$tmp_img = imagecreatetruecolor( $new_width, $new_height );
// copy and resize old image into new image
imagecopyresampled( $tmp_img, $img, 0, 0, 0, 0, $new_width, $new_height, $width, $height );
// save thumbnail into a file
imagejpeg( $tmp_img, "{$pathToThumb}{$fname}" );
}
You have to use that function like this:
createThumbs("path_to_image", "path_to_thumb", "thumb_width");
Replacing the arguments. Notice the word "path", it's a directory, like "../images/02", and you are using probably the path along with the picture name, like this:
createThumbs("images/008/01/0000288988r.jpg", " ......
And it should be:
createThumbs("images/008/01/" ...

imagejpeg() doesnt give the output properly in php

I want to upload an image from disk, resize it, and then upload it to Amazon S3.
However, I cant get the proper image output from imagejpeg().
heres my code:
$sourceUrl = $_FILES['path']['tmp_name'];
$thumbWidth = '100';
$thumbid = uniqid();
$img = imagecreatefromjpeg($sourceUrl);
$width = imagesx( $img );
$height = imagesy( $img );
// calculate thumbnail size
$new_width = $thumbWidth;
$new_height = floor( $height * ( $thumbWidth / $width ) );
// create a new temporary image
$tmp_img = imagecreatetruecolor( $new_width, $new_height );
// copy and resize old image into new image
imagecopyresampled($tmp_img, $img, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
// output the image
imagejpeg($tmp_img);
// upload thumbnail to s3
$s3->putObjectFile($tmp_img, "mybucket", $thumbid, S3::ACL_PUBLIC_READ);
Firebug gives me this error :
illegal character
[Break on this error] (�����JFIF���������>CREATOR: g...(using IJG JPEG v62), default quality\n
If I modify imagejpeg this way,
imagejpeg($tmp_img, 'abc.jpg');
then I get the same error. :(
Can i get some help here please ?
If you check the documentation of imagejpeg you can see it outputs the image, it means the way you call it it gets sent to the browser. You can get it to save to a file the second way you call it - by passing a filename in the second parameter.
Also, $tmp_img is an image resource, not a ready-to-use image file.
I don't know how your upload function works, but: if you need the file contents to upload, do it like this:
ob_start();
imagejpeg($tmp_image);
$image_contents = ob_get_clean();
$s3->putObjectFile($image_contents, "mybucket", $thumbid, S3::ACL_PUBLIC_READ);
if you need a filename to upload:
$filename = tempnam(sys_get_temp_dir(), "foo");
imagejpeg($tmp_image, $filename);
$s3->putObjectFile($filename, "mybucket", $thumbid, S3::ACL_PUBLIC_READ);
You have to define the header:
header('Content-type: image/jpeg');
1) $tmp_img is a resource not a file. You probably need to save the image to disc and use that for putObjectFile
2) You probably need to tell S3 that the file you're uploading is of type image/jpeg
Well guys thank you very much again, I screwed around a bit more and combining that with your responses I got this working as follows :)
$thumbid .= ".jpg";
$img = imagecreatefromgif($sourceUrl);
$width = imagesx( $img );
$height = imagesy( $img );
// calculate thumbnail size
$new_width = $thumbWidth;
$new_height = floor( $height * ( $thumbWidth / $width ) );
// create a new temporary image
$tmp_img = imagecreatetruecolor( $new_width, $new_height );
// copy and resize old image into new image
imagecopyresampled($tmp_img, $img, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
$path = '/var/www/1.4/wwwroot/cdn/'.$thumbid;
// output the image
if(imagegif($tmp_img, $path)){
$thumblink = "";
// upload thumbnail to s3
if($s3->putObjectFile($path, "mybucket", $thumbid, S3::ACL_PUBLIC_READ)){
$thumblink = "http://dtzhqabcdscm.cloudfront.net/".$thumbid;
imagedestroy($tmp_img);
}
return $thumblink;
}

Categories