PHP: Create Thumbnail Image Using X,Y Coordinates - php

How can I take a 500x500 (or any sized) image that has been uploaded to the server and generate a new image from defined specific x,y coordinates? For example (0,0) to (50,0); (0,50) to (50,50). Rather than resizing an image down to 50x50px, I want to grab the top left part of an image and in a sense "crop" it to use as a thumbnail.
How can I go about doing this in PHP?

You want to use imagecopy. First create an image with the dimensions you want and then use imagecopy to a portion of the source image into the new image:
// use whatever mechanism you prefer to load your source image into $image
$width = 50;
$height = 50;
// Define your starting coordinates in the source image.
$source_x = 20;
$source_y = 100;
$new_image = imagecreatetruecolor($width, $height);
imagecopy($new_image, $image, 0, 0, $source_x, $source_y, $width, $height);
// Now $new_image has the portion cropped from the source and you can output or save it.
imagejpeg($new_image);

http://www.php.net/manual/en/function.imagick-cropthumbnailimage.php#81547
$image = new Imagick($path."test1.jpg");
$image->cropThumbnailImage(160,120); // Crop image and thumb
$image->writeImage($path."test1.jpg");

I've seen a few ways to do this. If you want to generate the thumbs on the fly you could use:
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 */
imagecopyresized($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);
}
You can also set the quality parameter in the imagejpeg function.
Or if you want to save the image thumb to a directory I would look at:
http://bgallz.org/270/php-create-thumbnail-images/
or
http://www.imagemagick.org/script/index.php

Related

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

cache image on website increase browsing experience, load faster

Link http://i.stack.imgur.com/9F0q5.jpg
I am trying to make my webpages load more faster and better user experience, I am trying to resize those images in my website. My website has a lot of images
As for the image above, you can see the original size of the image (Natural Size) is 960 x 960 and the image displayed on the website is 214 x 214.
What if one of the images is huge, like 1920 x 1080, HD size. It will take a longer time to load the whole page waiting for the HD images to fully loaded.
Can anyone tell me the solution. I see many website has use image cache, and when click on the image, it will load the original image.
You need to work with thumbnails.
To perform this, you need where you put in your downsized pictures (=thumbnails). On the website, you only show the small pics, by clicking onto them you show the realsized pics (I suggest doing this with some kind of Javascript).
for storing smaller images(thumbnails):
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);
}
It uses GD functions of php.
For resizing image on client browser:
//on js
var thumbSize = 64;
var canvas = document.getElementById("canvas");
canvas.width = thumbSize;
canvas.height = thumbSize;
var c = canvas.getContext("2d");
var img = new Image();
img.onload = function(e) {
c.drawImage(this, 0, 0, thumbSize, thumbSize);//64x64
document.getElementById("yourImgID").src = canvas.toDataURL("image/png");
};
img.src = fileDataURL;
Courtesy:ref
Pick the best one that suits you

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

Crop Image with jWindowCrop - PHP

I'm using this jQuery plugin to crop images:
http://www.tmatthew.net/jwindowcrop
As you can see, it's really easy to use it on jQuery side, but my problem is with cropping the real image with PHP/GD.
with some goggling, I got:
$targ_w = $targ_h = 150;
$jpeg_quality = 90;
$src = 'demo_files/flowers.jpg';
$img_r = imagecreatefromjpeg($src);
$dst_r = ImageCreateTrueColor( $targ_w, $targ_h );
imagecopyresampled($dst_r,$img_r,0,0,$_POST['x'],$_POST['y'],
$targ_w,$targ_h,$_POST['w'],$_POST['h']);
header('Content-type: image/jpeg');
imagejpeg($dst_r, null, $jpeg_quality);
But it's not taking care of zoom ins/zoom outs made by the jQuery plugin, how I should crop the image and save it using this plugin and PHP?
I figured this out, here is my code in case of anyone else has the same question, the cropping will be done by Zebra image class:
http://stefangabos.ro/php-libraries/zebra-image/#documentation
PHP:
// The variables we got from the plugin in upload page:
$x = intval($_POST['x']);
$y = intval($_POST['y']);
$w = intval($_POST['w']);
$h = intval($_POST['h']);
// The img file which we want to crop
$tmp_file = 'path/to/img';
// Now include the Zebra class
include_once('path/to/Zebra_Image.php');
$image = new Zebra_Image();
$image -> preserve_aspect_ratio = true;
$image -> enlarge_smaller_images = true;
$image -> preserve_time = true;
$image -> jpeg_quality = 100;
// Now imagine that the user has selected the area which he want with the plugin, and we also want to make the image out put in a specific size(200*225):
$target_path = 'new/img/path'; // the output img path
$image -> source_path = $tmp_file;
$image -> target_path = $target_path;
$image -> crop($x, $y, $x + $w, $y + $h);
$image -> source_path = $target_path;
$image -> resize(200, 225, ZEBRA_IMAGE_CROP_CENTER);
I am also using jwindowcrop. When you click jwindowcrop's zoom, the w and h changes. (see attached picture)
You have to make sure you used the correct parameters as stated in the php manual
http://www.php.net/manual/en/function.imagecopyresampled.php
In my case, I used imagecopyresized and I could crop the image properly including the zooms
dst_image
Destination image link resource.
src_image
Source image link resource.
dst_x
x-coordinate of destination point.
(in my case the destination image should start from upper left corner)
dst_y
y-coordinate of destination point.
(in my case the destination image should start from upper left corner)
src_x
x-coordinate of source point.
(the x-coordinate returned by the cropping function e.g. crop image from x=231, 231 pixels far from the left edge)
src_y
y-coordinate of source point.
(the x-coordinate returned by the cropping function e.g. crop image from y=706, 706 pixels far from the top edge)
dst_w
Destination width.
(in my case, my new image should have a width of 800px)
dst_h
Destination height.
(in my case, my new image should have a height of 400px)
src_w
Source width.
(when my cropping function zooms, it changes the width and height of the original image)
src_h
Source height.
(when my cropping function zooms, it changes the width and height of the original image)
imagecopyresized(dst_image, src_image, 0, 0, 231, 706, 800, 400, 521, 318);

Creating virtual thumbnail of image with PHP

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.

Categories