How to generate image thumbnails in PHP in 300X200 dimensions? - php

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

Related

imagefttext() function (php) increases size of image

Trying to put watermark to an image
Image Original Size :- 10.8 MB
After watermarking:- New Image Size:- 37.9 MB
$text = "Afrophoto";
//the text i want as watermark
//create a new image
$newImg = imagecreatefromjpeg($image);
//set the watermark font color to red
$fontColor = imagecolorallocate($newImg, 255, 255, 255);
$font_file = 'fontArial.ttf';
list($width, $height) = getimagesize($image);
//write the watermark on the created image
imagefttext($newImg, 50, 50, $j, $i, $width - 100, $height - 100, $text);
//output the new image with a watermark to a file
imagejpeg($newImg,"uploads/".$_FILES[$field]['name'],100);
imagepng($newImg,"uploads/".$_FILES[$field]['name'].".png");
imagedestroy($newImg);
Here I used Png image
Actual size banned.png: 53.2KB
with watermark bbimage_3.png: 40.8 KB
<?php
$imageURL = "banned.png";
list($width,$height) = getimagesize($imageURL);
$imageProperties = imagecreatetruecolor($width, $height);
$targetLayer = imagecreatefrompng($imageURL);
imagecopyresampled($imageProperties, $targetLayer, 0, 0, 0, 0, $width, $height, $width, $height);
$WaterMarkText = 'CONFIDENTIAL';
$watermarkColor = imagecolorallocate($imageProperties, 191,191,191);
imagestring($imageProperties, 5, 130, 117, $WaterMarkText, $watermarkColor);
imagepng($imageProperties, 'bbimage_3.png');
header('Content-type: image/jpeg');
imagepng ($imageProperties);
imagedestroy($targetLayer);
imagedestroy($imageProperties);
?>
imagecopy: This function copies source image onto destination image by overwriting destination image pixels.
While merging png images with transparent background as a watermark, imagecopymerge() function will not preserve transparency onto the destination. So, imagecopy() is preferable for image watermarking.

Creating a thumbnail from an image [closed]

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

Partially working function for creating thumbnails

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

How to get src of upload file using php

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

centering image resource in new, larger image (PHP/imagecopy)

I'm trying to build a function that takes a PHP image resource and places it in the center of a new image of predetermined size. I do not want to scale the image; rather I want to place it as-is in the center of an enlarged "canvas."
$img is a valid image resource - if I return it I receive the correct original (unprocessed) image. $canvas_w and $canvas_h are the width and height of the desired new canvas. It's creating the correct size canvas, but the contents of the file are unexpectedly solid black when I return the desired "corrected" image resource ($newimg).
// what file?
$file = 'smile.jpg';
// load the image
$img = imagecreatefromjpeg($file);
// resize canvas (not the source data)
$newimg = imageCorrect($img, false, 1024, 768);
// insert image
header("Content-Type: image/jpeg");
imagejpeg($newimg);
exit;
function imageCorrect($image, $background = false, $canvas_w, $canvas_h) {
if (!$background) {
$background = imagecolorallocate($image, 255, 255, 255);
}
$img_h = imagesy($image);
$img_w = imagesx($image);
// create new image (canvas) of proper aspect ratio
$img = imagecreatetruecolor($canvas_w, $canvas_h);
// fill the background
imagefill($img, 0, 0, $background);
// offset values (center the original image to new canvas)
$xoffset = ($canvas_w - $img_w) / 2;
$yoffset = ($canvas_h - $img_h) / 2;
// copy
imagecopy($img, $image, $xoffset, $yoffset, $canvas_w, $canvas_h, $img_w, $img_h);
// destroy old image cursor
//imagedestroy($image);
return $img; // returns a black original file area properly sized/filled
//return $image; // works to return the unprocessed file
}
Any hints or obvious errors here? Thanks for any suggestions.
In place of imagecopy(), this seemed to work:
imagecopymerge($img, $image, $xoffset, $yoffset, 0,0, $img_w, $img_h, 100);

Categories