I need to help a friend to make his own artistic gallery.
I already have all done, but I need a tool/plugin/script to make him independent from me in uploading his own images. My gallery needs two images: a cropped one of a certain proportion (so i need him to crop by himself in an uploading page) and a thumb one (I want this be done automatically).
Do you know an easy way to do this? What would you do?
Thanks.
Personally i use this in all of my projects - http://www.verot.net/php_class_upload.htm
Works perfectly with upload files and with files that's already on the system.
Can do many things convert, resize and work on uploaded images in many ways, apply effects, add labels, watermarks and reflections and other image editing features.
Easy to work with.
If you're not going to be having heavy traffic to start - look at http://phpthumb.sourceforge.net/ which can create your resized images on the fly.
You need only GD library, function imagecopyresampled will suit you. PHP manual has really good example code for thumbnail creation http://php.net/manual/en/function.imagecopyresampled.php
You will need just to create exceptions for different file formats
function create_jpeg_thumbnail($thumbImageName,$imgSrc,$thumbDirectory,$thumbnail_width,$thumbnail_height) { //$imgSrc is a FILE - Returns an image resource.
$thumbDirectory = trim($thumbDirectory);
$imageSourceExploded = explode('/', $imgSrc);
$imageName = $imageSourceExploded[count($imageSourceExploded)-1];
$imageDirectory = str_replace($imageName, '', $imgSrc);
$filetype = explode('.',$imageName);
$filetype = strtolower($filetype[count($filetype)-1]);
//getting the image dimensions
list($width_orig, $height_orig) = getimagesize($imgSrc);
//$myImage = imagecreatefromjpeg($imgSrc);
if ($filetype == 'jpg') {
$myImage = imagecreatefromjpeg("$imageDirectory/$imageName");
} else
if ($filetype == 'jpeg') {
$myImage = imagecreatefromjpeg("$imageDirectory/$imageName");
} else
if ($filetype == 'png') {
$myImage = imagecreatefrompng("$imageDirectory/$imageName");
} else
if ($filetype == 'gif') {
$myImage = imagecreatefromgif("$imageDirectory/$imageName");
}
$ratio_orig = $width_orig/$height_orig;
if ($thumbnail_width/$thumbnail_height > $ratio_orig) {
$new_height = $thumbnail_width/$ratio_orig;
$new_width = $thumbnail_width;
} else {
$new_width = $thumbnail_height*$ratio_orig;
$new_height = $thumbnail_height;
}
$x_mid = $new_width/2; //horizontal middle
$y_mid = $new_height/2; //vertical middle
$process = imagecreatetruecolor(round($new_width), round($new_height));
imagecopyresampled($process, $myImage, 0, 0, 0, 0, $new_width, $new_height, $width_orig, $height_orig);
$thumb = imagecreatetruecolor($thumbnail_width, $thumbnail_height);
imagecopyresampled($thumb, $process, 0, 0, 0, 0, $thumbnail_width, $thumbnail_height, $thumbnail_width, $thumbnail_height);
//$thumbImageName = 'thumb_'.get_random_no().'.jpeg';
$destination = $thumbDirectory=='' ? $thumbImageName : $thumbDirectory."/".$thumbImageName;
imagejpeg($thumb, $destination, 100);
return $thumbImageName;
}
Related
This script below works fine to handle an uploaded image and resize it so that the max height or width (whichever side is longer) is 200px. So it could be 200x200 if it's perfect square image, or 200x140, or 140x200, etc.
if(isset($_FILES['image'])) {
$img = $_FILES['image']['name'];
$tmp = $_FILES['image']['tmp_name'];
// get uploaded file's extension
$ext = strtolower(pathinfo($img, PATHINFO_EXTENSION));
//checking if image exists for this pool and removing if so, before adding new image in its place
if(file_exists("uploads/".$poolid.".png")) {
unlink("uploads/".$poolid.".png");
}
// checks valid format
if(in_array($ext, $valid_extensions)) {
//re-size the image and make it a PNG before sending to server
$final_image = $poolid . ".png";
$path = "uploads/".strtolower($final_image);
$size = getimagesize($tmp);
$ratio = $size[0]/$size[1]; // width/height
if( $ratio > 1) {
$width = 200;
$height = 200/$ratio;
}
else {
$width = 200*$ratio;
$height = 200;
}
$src = imagecreatefromstring(file_get_contents($tmp));
$dst = imagecreatetruecolor($width,$height);
imagecopyresampled($dst,$src,0,0,0,0,$width,$height,$size[0],$size[1]);
imagedestroy($src);
imagepng($dst, $path); // adjust format as needed
imagedestroy($dst);
$_SESSION['image_uploaded']="yes";
echo $path ."?".rand(1,32000);
} else {
echo 'invalid file';
}
}
Now, Facebook sharing using OpenGraph requires an image to be at least 200x200. So a 140x200 image wouldn't work with their sharing functionality.
I don't love non-square images anyway, so I would like to take the image and if it's not already a square, I'd like to add whitespace to the sides (or on the top/bottom) and save it as a perfect 200x200 square every single time.
I tried this below, but it's not working (no image gets created at all). What is wrong with what I tried to do? This doesn't seem overly complicated but clearly I'm missing something.
if(isset($_FILES['image'])) {
$img = $_FILES['image']['name'];
$tmp = $_FILES['image']['tmp_name'];
// get uploaded file's extension
$ext = strtolower(pathinfo($img, PATHINFO_EXTENSION));
//checking if image exists for this pool and removing if so, before adding new image in its place
if(file_exists("uploads/".$poolid.".png")) {
unlink("uploads/".$poolid.".png");
}
// checks valid format
if(in_array($ext, $valid_extensions)) {
//re-size the image and make it a PNG before sending to server
$final_image = $poolid . ".png";
$path = "uploads/".strtolower($final_image);
$size = getimagesize($tmp);
$ratio = $size[0]/$size[1]; // width/height
if( $ratio > 1) {
$width = 200;
$height = 200/$ratio;
}
else {
$width = 200*$ratio;
$height = 200;
}
$src = imagecreatefromstring(file_get_contents($tmp));
$dst = imagecreatetruecolor($width,$height);
$orig_img=imagecopyresampled($dst,$src,0,0,0,0,$width,$height,$size[0],$size[1]);
imagedestroy($src);
// create new image and fill with background colour
$new_img = imagecreatetruecolor($output_w, $output_h);
$bgcolor = imagecolorallocate($new_img, 255, 0, 0); // red
imagefill($new_img, 0, 0, $bgcolor); // fill background colour
// copy and resize original image into center of new image
$final_img=imagecopyresampled($new_img, $orig_img, 0, 0, 0, 0, 200, 200, $width, $height);
imagepng($final_img, $path); // adjust format as needed
imagedestroy($dst);
$_SESSION['image_uploaded']="yes";
echo $path ."?".rand(1,32000);
} else {
echo 'invalid file';
}
}
You don't need a temporary intermediate image. You can paste the resampled source image right into the destination image after you fill it with background. See here:
$src = imagecreatefromstring(file_get_contents($tmp));
// Create new image and fill it with background color
$dst = imagecreatetruecolor($output_w,$output_h);
$bgcolor = imagecolorallocate($dst, 255, 0, 0);
imagefill($dst, 0, 0, $bgcolor);
// Copy resampled src image into dst
if ($ratio > 1)
imagecopyresampled($dst, $src, 0, ($output_h - $height) / 2, 0, 0, $width, $height, $size[0], $size[1]);
else
imagecopyresampled($dst, $src, ($output_w - $width) / 2, 0, 0, 0, $width, $height, $size[0], $size[1]);
imagepng($dst, $path); // adjust format as needed
imagedestroy($src);
imagedestroy($dst);
I'm using a php script to upload and resize an image, pretty simple:
if($_SERVER["REQUEST_METHOD"] == "POST") {
$image = $_FILES["image_upload"];
$uploadedfile = $image['tmp_name'];
if ($image) {
$filename = stripslashes($_FILES['image_upload']['name']);
$extension = getExtension($filename);
$extension = strtolower($extension);
if (($extension != "jpg") && ($extension != "jpeg") && ($extension != "png") && ($extension != "gif")) {
$error_txt = 'Immagine incoretta';
$errors=1;
} else {
$size=filesize($uploadedfile);
if ($size > MAX_SIZE*1024) {
$error_txt = "Immagine troppo grande";
$errors=1;
}
if($extension=="jpg" || $extension=="jpeg" ) {
$uploadedfile = $uploadedfile;
$src = imagecreatefromjpeg($uploadedfile);
} else if($extension=="png") {
$uploadedfile = $uploadedfile;
$src = imagecreatefrompng($uploadedfile);
} else {
$src = imagecreatefromgif($uploadedfile);
}
list($width,$height)=getimagesize($uploadedfile);
$newwidth=500;
$newheight=375;
$tmp=imagecreatetruecolor($newwidth,$newheight);
imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight,$width,$height);
$filename = "images/". generateRandomString(5) . $image['name'];
imagejpeg($tmp,$filename,100);
imagedestroy($src);
imagedestroy($tmp);
}
}
I want to got a bit further, right now im just resizing the image no matter the proportions, the think is, i want to resize it to a fixed with and height without losing the original proportion, and this of course is achieved through the cropping+resize of the original image.
I have no idea how to do this using my actual imagecreatetruecolor and imagecopyresampled functions, and looking to the php manual seems is not very easy.
There is a very good library im trying to integrate to my code, the use its as simple as mysite.com/lib/timthumb.php?src=castle1.jpg&h=180&w=120 but i dont know how to integrate that with my actual code.
So, what do you suggest?
Please forgive me if there are any typos or anything in the following code. I haven't tested it. What I've done here is calculate whether the height or the width is the proportion that's too long. Then adjust the source dimension to match the final image dimension. Also, adjust the center of the side that we shrunk so the cropped image is centered.
$newwidth = 500;
$newheight = 375;
$tmp = imagecreatetruecolor($newwidth, $newheight);
$widthProportion = $width / $newwidth;
$heightProportion = $height / $newheight;
if ($widthProportion > $heightProportion) {
// width proportion is greater than height proportion
// figure out adjustment we need to make to width
$widthAdjustment = ($width * ($widthProportion - $heightProportion));
// Shrink width to proper proportion
$width = $width - $widthAdjustment;
$x = 0; // No adjusting height position
$y = $y + ($widthAdjustment / 2); // Center the adjustment
} else {
// height proportion is greater than width proportion
// figure out adjustment we need to make to width
$heightAdjustment = ($height * ($heightProportion - $widthProportion));
// Shrink height to proper proportion
$height = $height - $heightAdjustment;
$x = $x + ($heightAdjustment / 2); // Center the ajustment
$y = 0; // No adjusting width position
}
imagecopyresampled($tmp, $src, 0, 0, $x, $y, $newwidth, $newheight, $width, $height);
So basically with the $width and $height variables you are specifying how much of the picture you want (cropping). and with the $x, $y we're saying where we want to crop the picture. The rest is just the standard resizing to fit the full new image.
I hope that helps!
I'm trying to write a simple file uploader which can resize an image if needed and copy it into the server's file system. That works if I pass the file directly but I also want to be able to pass a URL via a GET parameter. And somehow imagecopyresampled seems to fail with the URL.
Here is my code:
if(isset($_FILES["file"]))
$fn = $_FILES['file']['tmp_name'];
else
$fn = urldecode($_GET['url']);
$fileMD5 = md5_file($fn);
$target = $basedir . $fileMD5 . ".png";
list($width, $height, $imgtype) = getimagesize($fn);
if ($imgtype == IMAGETYPE_PNG)
$img = imagecreatefrompng($fn);
else if ($imgtype == IMAGETYPE_JPEG)
$img = imagecreatefromjpeg($fn);
else if ($imgtype == IMAGETYPE_GIF)
$img = imagecreatefromgif($fn);
else if ($imgtype == IMAGETYPE_BMP)
$img = imagecreatefromwbmp($fn);
else {
echo "unsupported file format";
return;
}
// image resize
if($width > $maxwidth && $width >= $height) {
$newwidth = $maxwidth;
$newheight = ($height / $width) * $newwidth;
} else if($height > $maxheight) {
$newheight = $maxheight;
$newwidth = ($width / $height) * $newheight;
}
$tmp = imagecreatetruecolor($newwidth, $newheight);
imagecopyresampled($tmp, $img, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
imagepng($tmp, $target);
Everything seems to work fine until imagecopyresampled. I get the correct IMAGETYPE and also, something is created with imagecreatetruecolor. But imagecopyresampled returns false. I'm pretty confused since the script seems to be able to actually read the image and get it's first bits to determine the type of it.
Any suspections what's going wrong?
Thanks in advance.
Your resize section seems to leave out some situations that could lead to an undefined $newheight and $newwidth.
Are you sure about imagecreatetruecolor returning an image?
I have a quick question that I'm not quite sure to set up. I've seen examples elsewhere but nothing specifically like my situation. I would like to resize images using PHP so they're readable and not just wonkily stretched like if you use HTML. If they're not 250 pixels wide, or 160 pixels tall, how can I resize the picture so it's proportionate but fits within that space?
Thanks!
PHP does not manipulate images directly. You will need to use an image manipulation library such as gd or ImageMagick to accomplish this goal.
In ImageMagick, image resizing is accomplished like this:
$thumb = new Imagick('myimage.gif');
$thumb->resizeImage(320,240,Imagick::FILTER_LANCZOS,1);
$thumb->writeImage('mythumb.gif');
With GD, you can do it like this:
<?php
// The file
$filename = 'test.jpg';
$percent = 0.5;
// Content type
header('Content-Type: image/jpeg');
// Get new dimensions
list($width, $height) = getimagesize($filename);
$new_width = $width * $percent;
$new_height = $height * $percent;
// Resample
$image_p = imagecreatetruecolor($new_width, $new_height);
$image = imagecreatefromjpeg($filename);
imagecopyresampled($image_p, $image, 0, 0, 0, 0, $new_width, $new_height, $width, $height);
// Output
imagejpeg($image_p, null, 100);
?>
Ok, so below is an Image object that I use in my store. It maintains scale - requires GD
<?php
class Store_Model_Image extends My_Model_Abstract
{
const PATH = STORE_MODEL_IMAGE_PATH;
const URL = "/store-assets/product-images/";
public function get_image_url($width, $height)
{
$old_file = self::PATH . $this->get_filename();
$basename = pathinfo($old_file, PATHINFO_FILENAME);
$new_name = sprintf("%s_%sx%s.jpg", $basename, $width, $height);
if(file_exists(self::PATH . $new_name))
{
return self::URL . $new_name;
}
else
{
list($width_orig, $height_orig, $image_type) = #getimagesize($old_file);
$img = FALSE;
// Get the image and create a thumbnail
switch($image_type)
{
case 1:
$img = #imagecreatefromgif($old_file);
break;
case 2:
$img = #imagecreatefromjpeg($old_file);
break;
case 3:
$img = #imagecreatefrompng($old_file);
break;
}
if(!$img)
{
throw new Zend_Exception("ERROR: Could not create image handle from path.");
}
// Build the thumbnail
if($width_orig > $height_orig)
{
$width_ratio = $width / $width_orig;
$new_width = $width;
$new_height = $height_orig * $width_ratio;
}
else
{
$height_ratio = $height / $height_orig;
$new_width = $width_orig * $height_ratio;
$new_height = $height;
}
$new_img = #imagecreatetruecolor($new_width, $new_height);
// Fill the image black
if(!#imagefilledrectangle($new_img, 0, 0, $new_width, $new_height, 0))
{
throw new Zend_Exception("ERROR: Could not fill new image");
}
if(!#imagecopyresampled($new_img, $img, 0, 0, 0, 0, $new_width, $new_height, $width_orig, $height_orig))
{
throw new Zend_Exception("ERROR: Could not resize old image onto new bg.");
}
// Use a output buffering to load the image into a variable
ob_start();
imagejpeg($new_img, NULL, 100);
$image_contents = ob_get_contents();
ob_end_clean();
// lastly (for the example) we are writing the string to a file
$fh = fopen(self::PATH . $new_name, "a+");
fwrite($fh, $image_contents);
fclose($fh);
return self::URL . $new_name;
}
}
}
I resize the image at request time, so the first time the page loads an image will be resized to the required size for the template. (this means I don't have to crash a shared host trying to regenerate image thumbnails everytime my design changes)
So in the template you pass your image object, and when you need a image thumb,
<img src="<?php echo $image->get_image_url(100, 100); ?>" />
you now have a 100x100 thumb, which is saved to the Server for reuse at a later date
gd and imagemagick are two tools that may work for you
http://php.net/manual/en/book.image.php
http://php.net/manual/en/book.imagick.php
Here is something I used to use
class cropImage{
var $imgSrc,$myImage,$cropHeight,$cropWidth,$x,$y,$thumb;
function setImage($image,$moduleWidth,$moduleHeight,$cropPercent = "1") {
//Your Image
$this->imgSrc = $image;
//getting the image dimensions
list($width, $height) = getimagesize($this->imgSrc);
//create image from the jpeg
$this->myImage = imagecreatefromjpeg($this->imgSrc) or die("Error: Cannot find image!");
if($width > $height) $biggestSide = $width; //find biggest length
else $biggestSide = $height;
//The crop size will be half that of the largest side
//$cropPercent = 1.55; // This will zoom in to 50% zoom (crop)
if(!$cropPercent) {
$cropPercent = 1.50;
}
$this->cropWidth = $moduleWidth*$cropPercent;
$this->cropHeight = $moduleHeight*$cropPercent;
//$this->cropWidth = $biggestSide*$cropPercent;
//$this->cropHeight = $biggestSide*$cropPercent;
//getting the top left coordinate
$this->x = ($width-$this->cropWidth)/2;
$this->y = ($height-$this->cropHeight)/2;
}
function createThumb($moduleWidth,$moduleHeight){
$thumbSize = 495; // will create a 250 x 250 thumb
$this->thumb = imagecreatetruecolor($moduleWidth, $moduleHeight);
//$this->thumb = imagecreatetruecolor($thumbSize, $thumbSize);
imagecopyresampled($this->thumb, $this->myImage, 0, 0,$this->x, $this->y, $moduleWidth, $moduleHeight, $this->cropWidth, $this->cropHeight);
//imagecopyresampled($this->thumb, $this->myImage, 0, 0,$this->x, $this->y, $thumbSize, $thumbSize, $this->cropWidth, $this->cropHeight);
}
function renderImage(){
header('Content-type: image/jpeg');
imagejpeg($this->thumb);
imagedestroy($this->thumb);
}
}
Call it by using
$image = new cropImage;
$image->setImage($imagepath,$moduleWidth,$moduleHeight,$scaleRelation);
$image->createThumb($moduleWidth,$moduleHeight);
$image->renderImage();
Use GD or ImageMagick. Here you may find a real production example of code (used by MediaWiki) that supports consoled ImageMagick interface (transformImageMagick method), ImageMagick extension interface (transformImageMagickExt method) and GD (transformGd method).
There a simple to use, open source library called PHP Image Magician that will can help you out.
Example of basis usage:
$magicianObj = new imageLib('racecar.jpg');
$magicianObj -> resizeImage(100, 200, 'crop');
$magicianObj -> saveImage('racecar_small.png');
I'm currently coding an uploader script based on Uploadify. Right now I resize the given image and watermark one of the sizes. It all works well, but I need the script to resize the height and then crop the width so that the aspect ratio does not get messed up.
This is my code so far:
if ($fileExtension == "jpg" ||
$fileExtension == "jpeg" ||
$fileExtension == "png" ||
$fileExtension == "gif"){
// GD variables:
list($width, $height, $type) = GetImageSize($uploadedFile['tmp_name']);
// Image sizes:
$bigImage = array(800, 453);
$mediumImage = array(410, 231);
$listImage = array(120, 68);
$thumbnail = array(90, 51);
$sourceAspect = $width / $height;
$bigAspect = $bigImage[0] / $bigImage[1];
$mediumAspect = $mediumImage[0] / $mediumImage[1];
$listAspect = $listImage[0] / $listImage[1];
$thumbnailAspect = $thumbnail[0] / $thumbnail[1];
// Image is PNG:
if ($type == IMAGETYPE_PNG){
$image = imagecreatefrompng($uploadedFile['tmp_name']);
$valid = true;
}
// Image is JPEG:
else if ($type == IMAGETYPE_JPEG){
$image = imagecreatefromjpeg($uploadedFile['tmp_name']);
$valid = true;
}
// Image is GIF:
else if ($type == IMAGETYPE_GIF){
$image = imagecreatefromgif($uploadedFile['tmp_name']);
$valid = true;
}
// Format not allowed:
else {
$valid = false;
}
// Start creating images:
if ($valid){
// Get size:
$imageSize = getimagesize($uploadedFile['tmp_name']);
// Generate canvas:
$bCanvas = imagecreatetruecolor($bigImage[0], $bigImage[1]);
$mCanvas = imagecreatetruecolor($mediumImage[0], $mediumImage[1]);
$lCanvas = imagecreatetruecolor($listImage[0], $listImage[1]);
$tCanvas = imagecreatetruecolor($thumbnail[0], $thumbnail[1]);
// Copy content:
imagecopyresampled($bCanvas, $image, 0, 0, 0, 0, ($bigImage[0] * $sourceAspect), ($bigImage[1] / $sourceAspect), $imageSize[0], $imageSize[1]);
imagecopyresampled($mCanvas, $image, 0, 0, 0, 0, $mediumImage[0], $mediumImage[1], $imageSize[0], $imageSize[1]);
imagecopyresampled($lCanvas, $image, 0, 0, 0, 0, $listImage[0], $listImage[1], $imageSize[0], $imageSize[1]);
imagecopyresampled($tCanvas, $image, 0, 0, 0, 0, $thumbnail[0], $thumbnail[1], $imageSize[0], $imageSize[1]);
// Save images:
$saveB = imagejpeg($bCanvas, $targetFile.'_big.jpg', 90);
$saveM = imagejpeg($mCanvas, $targetFile.'_medium.jpg', 90);
$saveT = imagejpeg($lCanvas, $targetFile.'_list.jpg', 90);
$saveT = imagejpeg($tCanvas, $targetFile.'_thumb.jpg', 90);
// Destroy images:
imagedestroy($image);
imagedestroy($bCanvas);
imagedestroy($mCanvas);
imagedestroy($lCanvas);
imagedestroy($tCanvas);
// Watermark images:
$mark = imagecreatefrompng("logo.png");
list($mwidth, $mheight) = getimagesize("logo.png");
$img = imagecreatefromjpeg($targetFile.'_big.jpg');
list($bwidth, $bheight) = getimagesize($targetFile.'_big.jpg');
imagecopy($img, $mark, $bwidth-$mwidth-25, $bheight-$mheight-25, 0, 0, $mwidth, $mheight);
imagejpeg($img, $targetFile.'_big.jpg', 100);
imagedestroy($img);
} else {
echo "0";
}
} else {
move_uploaded_file($tempFile,$targetFile.'.'.$fileExtension);
}
I would be really happy if someone could help me solve this. I've been trying several methods but none of them seemed to work properly. As you can see in the top I have already defined the canvas sizes that I want to use in the variables "bigImage", "mediumImage", "listImage" and "thumbnail".
Thanks in advance! //
Jonathan
There is more than one way to resize an image. I'll spell them out for you:
Stretch to fit -- the image is resized to the desired size ignoring aspect ratio
Scale to fit -- the image is resized so that one dimension (width or height) has the desired size while the other is same or shorter while maintaining aspect ratio (one extra step may be required to fill the shorter side with solid color)
Crop to fit -- the image is resized so that one dimension (width or height) has the desired size while the other is same or longer while maintaining aspect ratio (one extra step is required to trim the outside region)
PS: both articles were written by me.