Problem in creating thumbnail of png image ! - php

I have used the following php function to create thumbnail image.
function createThumbs( $pathToImages, $pathToThumbs, $thumbWidth )
{
$dir = opendir( $pathToImages );
while (false !== ($fname = readdir( $dir ))) {
$info = pathinfo($pathToImages . $fname);
if ( strtolower($info['extension']) == 'jpg' || strtolower($info['extension']) == 'png' )
{
// load image and get image size
if(strtolower($info['extension']) == 'jpg')
$img = imagecreatefromjpeg( "{$pathToImages}{$fname}" );
else
$img = imagecreatefrompng( "{$pathToImages}{$fname}" );
$width = imagesx( $img );
$height = imagesy( $img );
// calculate thumbnail size
$new_width = $thumbWidth;
$new_height = floor( $height * ( $thumbWidth / $width ) );
// create a new tempopary 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 );
imagecopyresampled($tmp_img, $img, 0, 0, 0, 0, $new_width, $new_height, $width, $height );
// save thumbnail into a file
if(strtolower($info['extension']) == 'jpg')
imagejpeg( $tmp_img, "{$pathToThumbs}{$fname}" );
else
imagepng( $tmp_img, "{$pathToThumbs}{$fname}" );
}
}
// close the directory
closedir( $dir );
}
Proper thumbnail is created for jpg image. But for png transparent image, thumbnail is created with black background. How do I make function work for png image? Please do suggest me. Thanks in advance.

I don't remember the exact particulars, but you'll want to read up on, and experiment with imagesavealpha() and imageaplphablending()
If memory serves me correctly, you'll want to set imagealphablending off, and then set imagesavealpha true. (In fact, yes, the manual page for imagesavealpha() implies just that)
So, at the end of your code:
// save thumbnail into a file
if(strtolower($info['extension']) == 'jpg'){
imagejpeg( $tmp_img, "{$pathToThumbs}{$fname}");
}else{
imagealphablending($tmp_img,false);
imagesavealpha($tmp_img,true);
imagepng( $tmp_img, "{$pathToThumbs}{$fname}" );
}

Are you trying to reinvent the wheel? Use php Thumbnailer :)
<?php
// load the library
require 'Thumbnailer';
// make callback function
function myfunc(& $thumb) {
// that will make a image thumbnail square 100x100px
$thumb->thumbSquare(100)->save('photos/output/'.$thumb->filename);
}
// call batch helper
// find all jpg, png and gif images in /photos/directory
Thumbnailer::batch('myfunc', '/photos/directory/*.{jpg,png,gif}');
?>
That's easy.

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)

Create a circle thumbnail from jpeg in PHP

Hi currently I built have function to create a thumbnail from an image and it does so, however I want it to be a circle thumbnail not a rectangle, how can I achieve this without using any external libraries just php built in methods? thank you
function createThumb( $imagepath, $thumbFile, $thumbWidth )
{
// load image and get image size
$img = imagecreatefromjpeg( "$imagepath" );
$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
imagecopyresized( $tmp_img, $img, 0, 0, 0, 0,
$new_width, $new_height, $width, $height );
// save thumbnail into a file in the temp directory below script or somewhere
imagejpeg( $tmp_img, $thumbFile );
}
CSS is a better way to do it actually .
.class-name {
border-radius : 30px;
}

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/" ...

create thumbnail of image and then save it into database

i am creating a thumbnail of an image and then save it into the database but i don't know how to do so. here is my code
function createThumbs( $pathToImages, $pathToThumbs, $thumbWidth )
{
$pathToImages = "../uploads/images/";
// open the directory
$dir = opendir( $pathToImages );
// loop through it, looking for any/all JPG files:
while (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' )
{
$pathToThumbs = "../uploads/images/thumbs/";
//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
imagecopyresized( $tmp_img, $img, 0, 0, 0, 0, $new_width,$new_height, $width, $height );
// save thumbnail into a file
imagejpeg( $tmp_img, "{$pathToThumbs}{$fname}" );
$thumbimage = $fname;
}
}
// close the directory
closedir( $dir );
}
createThumbs("/uploads/documents","/uploads/images/thumbs/",100);
creation of thumbnail is fine but now i dont know how to save it in db(lack of skills)
why you don't save the results in file?
i think you must select the text type in database field, then save the content of image there,
and read the data in a diffrent php file with "Content-type: image/jpg" header.
for example:
$query = "SELECT image FROM youtablename WHERE id = 10";
$result = mysql_query($query) or die('Error, query failed');
$content=mysql_result($result,0,"image");
header("Content-type: image/jpg");
echo $content;
You can use the Dropbox API that automatically generates thumbnails. I think that save binary files in a database is the worst mistake we can make. Especially for the performance and files management becomes difficult.

Categories