Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I'm trying to create a thumbnail from an image, but it's not working for some reason. What am I doing wrong?
<?php
function make_thumb($src, $dest, $desired_width) {
/* read the source image */
$source_image = imagecreatefromjpeg($src);
$width = imagesx($source_image);
$height = imagesy($source_image);
/* find the "desired height" of this thumbnail, relative to the desired width */
$desired_height = floor($height * ($desired_width / $width));
/* create a new, "virtual" image */
$virtual_image = imagecreatetruecolor($desired_width, $desired_height);
/* copy source image at a resized size */
imagecopyresampled($virtual_image, $source_image, 0, 0, 0, 0, $desired_width, $desired_height, $width, $height);
/* create the physical thumbnail image to its destination */
imagejpeg($virtual_image, $dest);
} //end of function make_thumb($src, $dest, $desired_width)
make_thumb("uploads/643Full-HD-Space-Wallpapers-Widescreen.jpg", "test", 100);
?>
Your function works for me, however, I had to give the destination a jpeg extension for it to be a valid jpeg image:
//---------------------------------------------------------------------vvv
make_thumb("uploads/643Full-HD-Space-Wallpapers-Widescreen.jpg", "test.jpg", 100);
EDIT:
Based on your comment, this would be the full function:
function make_thumb($src, $dest, $desired_width)
{
// Make directory if not made
if(!is_dir($dest))
mkdir($dest,0755,true);
// Get path info
$pInfo = pathinfo($src);
// Save the new path using the current file name
$dest = $dest."/".$pInfo['basename'];
// Do the rest of your stuff and things...
$source_image = imagecreatefromjpeg($src);
$width = imagesx($source_image);
$height = imagesy($source_image);
$desired_height = floor($height * ($desired_width / $width));
$virtual_image = imagecreatetruecolor($desired_width, $desired_height);
imagecopyresampled($virtual_image, $source_image, 0, 0, 0, 0, $desired_width, $desired_height, $width, $height);
imagejpeg($virtual_image, $dest);
}
// Create file here
make_thumb("http://res.cloudinary.com/demo/image/upload/w_250,q_90/happy_dog.jpg", "test", 100);
Related
I have a partially working function for making thumbnails but 10% of the images doesn't get created as thumbnails, and they're the same exact 10%. The other 90% works. I'm not sure why though. Please take a look at my code:
<?php
$image = "511photo.jpg";
if ($image) {
make_thumb("uploads", "thumbnails", $image, 500);
}
function make_thumb($imageFrom, $imageTo, $image, $thumbWidth) {
/* read the source image */
$getFrom = $imageFrom."/".$image;
$source_image = imagecreatefromjpeg($getFrom);
$width = imagesx($source_image);
$height = imagesy($source_image);
/* find the "desired height" of this thumbnail, relative to the desired width */
$thumbHeight = floor($height * ($thumbWidth / $width));
/* create a new, "virtual" image */
$virtual_image = imagecreatetruecolor($thumbWidth, $thumbHeight);
/* copy source image at a resized size */
imagecopyresampled($virtual_image, $source_image, 0, 0, 0, 0, $thumbWidth, $thumbHeight, $width, $height);
/* create the physical thumbnail image to its destination */
$dest = $imageTo."/".$image;
imagejpeg($virtual_image, $dest);
} //end of function make_thumb($imageFrom, $imageTo, $image, $thumbWidth)
?>
Note: Here's a couple of other $image that doesn't work:
"434cute-anime-couple-drawing-on-tumblr.png"
"503anime_head_vectorized_by_cona_cru-d784ls0.png"
Note: Yes, I am sure that they are all in the uploads folder - I've checked and double checked so honestly, right now I'm so confuse...
It's because you are using imagecreatefromjpeg() for png image. You need to use imagecreatefrompng() for these images.
$source_image = imagecreatefrompng($getFrom);
For checking image type you can use exif_imagetype() function :
$imageType = exif_imagetype($getFrom);
if($imageType == IMAGETYPE_PNG) {
//It's PNG
} elseif($imageType == IMAGETYPE_JPEG) {
//It's JPEG
} //You can check more types here.
check file type and add this code with imagejpeg function in a condition its an example add your variables and values
if($fileType=="image/png"){
$im=ImageCreateFromPNG($add);
$width=ImageSx($im); // Original picture width is stored
$height=ImageSy($im); // Original picture height is stored
$newimage=imagecreatetruecolor($n_width,$n_height);
imageCopyResized($newimage,$im,0,0,0,0,$n_width,$n_height,$width,$height);
ImagePng($newimage,$tsrc);
chmod("$tsrc",0777);
}
I am trying to create thumbnail of previously uploaded images using this function.
Original images are uploaded to mysite/used_uploads and thumbnails should be created at mysite/used_uploads_thb.
The thumbnail function is triggered directly after upload of the original.
I have also changed permissions with the directory, as follows, but the problem persists.
chmod("used_uploads_thb", 0777);
The directories are as follows:
mysite/used_uploads
mysite/used_uploads_thb
This is the whole script. The last step is giving the above error.
<?php
$src = substr($filePath, 1);
//$src example: used_uploads/252-558ec2e5dc45c-alfa-romeo-giulia - 2.jpg
chmod("used_uploads_thb", 0777);
$dest = '/used_uploads_thb';
$desired_width="100";
function make_thumb($src, $dest, $desired_width) {
/* read the source image */
$source_image = imagecreatefromjpeg($src);
$width = imagesx($source_image);
$height = imagesy($source_image);
/* find the "desired height" of this thumbnail, relative to the desired width */
$desired_height = floor($height * ($desired_width / $width));
/* create a new, "virtual" image */
$virtual_image = imagecreatetruecolor($desired_width, $desired_height);
/* copy source image at a resized size */
imagecopyresampled($virtual_image, $source_image, 0, 0, 0, 0, $desired_width, $desired_height, $width, $height);
/* create the physical thumbnail image to its destination */
imagejpeg($virtual_image, $dest);
print_r(error_get_last());
}
make_thumb($src, $dest, $desired_width);
?>
This is the error message:
Array
(
[type] => 2
[message] => imagejpeg(/used_uploads_thb): failed to open stream: Permission denied
[file] => /Applications/MAMP/htdocs/SiteCar/used_thumbnail.php
[line] => 26
)
I appreciate your help.
Just for the record.
The issue was with the destination path for the thumbnail. My original code had only the directory. I was wrongly assuming that the name would be the same as the original file and would be automatically created. Not so.
So here it is the working code:
The preg_replace is there only because I am placing the thumbnails in a separate directory to the original image.
<?php
$src = $new_name = $filePath;
$new_name = preg_replace('/used_uploads\/(.*)$/', '$1', $new_name);
$src = $_SERVER['DOCUMENT_ROOT'] . $src;
$dest = 'used_uploads_thb'. $new_name;
$desired_width="100";
function make_thumb($src, $dest, $desired_width) {
/* read the source image */
$source_image = imagecreatefromjpeg($src);
$width = imagesx($source_image);
$height = imagesy($source_image);
/* find the "desired height" of this thumbnail, relative to the desired width */
$desired_height = floor($height * ($desired_width / $width));
/* create a new, "virtual" image */
$virtual_image = imagecreatetruecolor($desired_width, $desired_height);
/* copy source image at a resized size */
imagecopyresampled($virtual_image, $source_image, 0, 0, 0, 0, $desired_width, $desired_height, $width, $height);
/* create the physical thumbnail image to its destination */
imagejpeg($virtual_image,$dest);
//print_r(error_get_last());
}
make_thumb($src, $dest, $desired_width);
?>
Make sure you have your both directory permission is set to 0777 Or your source image filename never use space if you work on linux because you need to add escape char of space '\ ' on every space from your filename, make sure after upload you rename it to something using
$src = md5('252-558ec2e5dc45c-alfa-romeo-giulia - 2.jpg') . '.jpg';
I'm using the below code to generate image thumbnails in PHP. It generates the thumbnails proportional to the image height and width dimensions.
make_thumb('images/image.jpg', 'images-generated-thumbs/7.jpg', 300, 200);
function make_thumb($src, $dest, $desired_width, $desired_height) {
/* read the source image */
$source_image = imagecreatefromjpeg($src);
$width = imagesx($source_image);
$height = imagesy($source_image);
/* find the "desired height" of this thumbnail, relative to the desired width */
$desired_height = floor($height*($desired_width/$width));
$desired_width = floor($width*($desired_height/$height));
/* create a new, "virtual" image */
$virtual_image = imagecreatetruecolor($desired_width, $desired_height);
/* copy source image at a resized size */
imagecopyresampled($virtual_image, $source_image, 0, 0, 0, 0, $desired_width, $desired_height, $width, $height);
/* create the physical thumbnail image to its destination */
imagejpeg($virtual_image, $dest);
}
For the above example, it generates the 7.jpg thumbnail with 299x187 in size. So, my question is how to fill the rest of the pixels ((300-299)x(300-187)) in white color.
If we remove the $desired_height variable in above code, it exactly generates a thumbnail with 300 in width, so only need is to fill the rest of the height with the white color.
Before you modify the width/height, store them:
$actual_width = $desired_width;
$actual_height = $desired_height;
$desired_height = floor($height*($desired_width/$width));
$desired_width = floor($width*($desired_height/$height));
When you are doing the canvas:
/* create a new, "virtual" image */
$virtual_image = imagecreatetruecolor($actual_width, $actual_height);
Virtual image at this point is black, fill it with white:
$white = imagecolorallocate($virtual_image, 255, 255, 255);
imagefill($virtual_image, 0, 0, $white );
I am trying to use a function to create thumbnails for which I need the src of the uploaded image. How can I get that?
Following is the function that I'm interested in:
function make_thumb($src, $dest, $desired_width) {
/* read the source image */
$source_image = imagecreatefromjpeg($src);
$width = imagesx($source_image);
$height = imagesy($source_image);
/* find the "desired height" of this thumbnail, relative to the desired width */
$desired_height = floor($height * ($desired_width / $width));
/* create a new, "virtual" image */
$virtual_image = imagecreatetruecolor($desired_width, $desired_height);
/* copy source image at a resized size */
imagecopyresampled($virtual_image, $source_image, 0, 0, 0, 0, $desired_width, $desired_height, $width, $height);
/* create the physical thumbnail image to its destination */
imagejpeg($virtual_image, $dest);
}
Your help in this regard will be appreciated.
$src will be your original uploaded image
$dest will be the resized image
These will both be file system paths. Assuming that both of these paths exist under the document root you can translate those to a url by just trim off the $_SERVER["DOCUMENT_ROOT"]
$url = str_replace($_SERVER["DOCUMENT_ROOT"], realpath($dest));
The actual image source is in $source_image (it will be a resource).
You can always file_get_contents($src) if you wanted the raw data...
I want to display the thumbnail of image while displaying time,but dont want to save the image.
I have tried some notorious ;) script but its not working :(. Please have look and let me know do you have any idea
<?php
function print_thumb($src, $desired_width = 100){
/* read the source image */
$source_image = imagecreatefromjpeg($src);
$width = imagesx($source_image);
$height = imagesy($source_image);
/* find the "desired height" of this thumbnail, relative to the desired width */
$desired_height = floor($height * ($desired_width / $width));
/* create a new, "virtual" image */
$virtual_image = imagecreatetruecolor($desired_width, $desired_height);
/* copy source image at a resized size */
imagecopyresampled($virtual_image, $source_image, 0, 0, 0, 0, $desired_width, $desired_height, $width, $height);
// Set the content type header - in this case image/jpeg
header('Content-Type: image/jpeg');
// Output the image
imagejpeg($virtual_image);
}
?>
<img src="<?php print_thumb("s1.jpg"); ?>" />
I saved this file as thumbs.php (single file) while accessing it via localhost it displaying like
<img src="http://localhost/test/thumbs.php">
Its working fine if I write both in separate file.
like file.html with
<img src="thumb.php?img=s1.jpg" />
and
thumb.php
<?php
function print_thumb($src, $desired_width = 100){
/* read the source image */
$source_image = imagecreatefromjpeg($src);
$width = imagesx($source_image);
$height = imagesy($source_image);
/* find the "desired height" of this thumbnail, relative to the desired width */
$desired_height = floor($height * ($desired_width / $width));
/* create a new, "virtual" image */
$virtual_image = imagecreatetruecolor($desired_width, $desired_height);
/* copy source image at a resized size */
imagecopyresampled($virtual_image, $source_image, 0, 0, 0, 0, $desired_width, $desired_height, $width, $height);
// Set the content type header - in this case image/jpeg
header('Content-Type: image/jpeg');
// Output the image
imagejpeg($virtual_image);
}
print_thumb($_REQUEST['img']);
?>
You need 2 separate scripts.
1 to output the image image/jpg and one to output the HTML. You appear to be trying to render the image directly to the src attribute.
HTML Page:
<html>
<body>
<img src="http://localhost/test/thumbs.php?img=s1.jpg">
</body>
</html>
PHP Page:
<?php
function print_thumb($src, $desired_width = 100){
// your function as is
}
// you should probably sanitize this input
print_thumb($_REQUEST['img']);
?>
Try using "TimThumb" (CLICK HERE), it makes things like this very easy indeed.
If <img src="<?php print_thumb("s1.jpg"); ?>" /> is part of thumbs.php as in your shown code, then you have to remove it. I also suggest removing ?> from thumbs.php
Inside 'thumb.php'
<?php
function print_thumb($src, $desired_width = 100){
if( !file_exists($src) )
return false;
/* read the source image */
$source_image = imagecreatefromjpeg($src);
$width = imagesx($source_image);
$height = imagesy($source_image);
/* find the "desired height" of this thumbnail, relative to the desired width */
$desired_height = floor($height * ($desired_width / $width));
/* create a new, "virtual" image */
$virtual_image = imagecreatetruecolor($desired_width, $desired_height);
/* copy source image at a resized size */
imagecopyresampled($virtual_image, $source_image, 0, 0, 0, 0, $desired_width, $desired_height, $width, $height);
// Set the content type header - in this case image/jpeg
header('Content-Type: image/jpeg');
// Output the image
imagejpeg($virtual_image);
return true;
}
if( isset( $_GET['file'] ) && !empty( $_GET['file'] ) ){
if( !print_thumb( $_GET['file'] ) ){
echo 'Failed to create thumb for "'.$_GET['file'].'"';
}else{
// The Thumb would have been returned
}
}else{
echo 'No File specified';
}
?>
Inside any page displaying the thumbnails
<img src="thumb.php?file=s1.jpg" />
The problem you have with your existing code is that you are putting the wrong code in the wrong file. The thumb.php file is meant to do nothing but look at the parameters it is being sent (the file it is meant to turn into a thumbnail) and return the image.
So putting the <img.... markup at the end of that file is where your problem is.
Instead, you have to look at where you are trying to make the images appear. And you have to pass that image filename into the markup, so the thumbs.php file know what you want it to thumbnail and return.
As another respondant noted, there are libraries which will do this for you alot easier than writing it up yourself.