how to take image from one folder and rename and re-size the images and move to another folder?
I need to get image from one folder and rename and re-size the same image and move into another folder using php
You will most likely be using gd for resizing the images.
Here is a pretty crappy, but hopefully useful code sample. In this case, $originalName is the name given in the $_FILES array's tmp_name position. I am resizing to a width of 1200 in this case, with the height adapting according to such width. You might (and most likely will) not desire this behavior. This is just some ugly code I used in some courses I taught about 3 years ago, I don't have the newer samples in this computer so you will have to get used to the idea :)
$newDir is where the file will be located. by calling imagejpeg or imagepng and passing the filename as second argument, it means to the function that you wish to save the image in that location.
if ($type == 'image/jpeg') {
$original = imagecreatefromjpeg($originalName);
}
else {
$original = imagecreatefrompng($originalName);
}
$width = imagesx($original);
$height = imagesy($original);
//prepare for creation of image with width of 1000
$new_height = floor($height * (1200 / $width));
// create the 1200 width image
$tmp_img = imagecreatetruecolor(1200, $new_height);
// copy and resize old image into new image
imagecopyresized($tmp_img, $original, 0, 0, 0, 0,
1200, $new_height, $width, $height);
//create a random and unique name to identify (here it isn't that random ;)
$newDir = '/this/is/some/directory/and/filename.';
if ($type == 'image/jpeg') {
imagejpeg($tmp_img, $newDir."jpg");
}
else {
imagepng($tmp_img, $newDir."png");
}
Many file system functions are already built-in with PHP (e.g. rename), and you'll find most of what you need to resize images by using the GD library here.
There are libraries available in PHP for image resize.
Here are some useful links you may like.
http://www.fliquidstudios.com/2009/05/07/resizing-images-in-php-with-gd-and-imagick/
http://php.net/manual/en/book.image.php
PHP/GD - Cropping and Resizing Images
http://net.tutsplus.com/tutorials/php/image-resizing-made-easy-with-php/
Use imagick http://php.net/manual/en/imagick.resizeimage.php
If I were you I would write a PE using a diffent language (one that you might be best used to) to adjust anything of the given image then just feel free to phpexec it to do all the steps you mentioned, you can sit relax and wait for the end result. HHAHAHHA :-)
Use imagick library to resize; it's good.
Related
I posted earlier about another resizing script not working and I got a little farther with this script which does things a little differently.
I got a little farther, only now there is a new problem. The first three lines of the code successfully place three identical files in the target directory with the file and it's two thumbnail files named accordingly. I then want to load the thumbnails, which are still full-size, and resize them but the script stops at imagecreatefromjpeg() and I can't seem to figure out why because $src has a value.
I thought that I could possibly remove that line and replace $source with $src in my imagecopyresized() function, and that gets me even closer. But it then returns a thumbnail of the target size, but the thumbnail is black.
move_uploaded_file($tmpFilePath, $newFilePath);
copy($newFilePath, $thumb500);
copy($newFilePath, $thumb200);
function thumbImage($src, $dest, $newheight) {
list($width, $height) = getimagesize($src);
$newwidth = $width * ($newheight / $height);
// Load
$thumb = imagecreatetruecolor($newwidth, $newheight);
header('Content-type: image/jpeg');
$source = imagecreatefromjpeg($src);
// Resize
imagecopyresized($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);
// Output
imagejpeg($thumb, $dest);
}
thumbImage($thumb500, $thumb500, 500);
thumbImage($thumb200, $thumb200, 200);
I feel as if this must be a common issue. Any suggestions anyone?
For me the supplied code block works if I use a JPEG image as source.
The problem may be that you use a PNG image that uses transparency. As JPEG can not handle transparencies, the transparent background colour will be filled. Maybe that is the problem. If not, please provide a sample image that has the problematic behaviour.
5
imagecopyresized takes an image resource as its second parameter, not a file name. You'll need to load the file first. If you know the file type, you can use imagecreatefromFILETYPE to load it. For example, if it's a JPEG, use imagecreatefromjpeg and pass that the file name - this will return an image resource.
If you don't know the file type, all is not lost. You can read the file in as a string and use imagecreatefromstring (which detects file types automatically) to load it as follows:
{$oldImage = imagecreatefromstring(file_get_contents($_FILES['image']['tmp_name']));
}
enter code here
I have a Folder called "Gallery" inside which i have created a folder for Thumbails called "Gallery-Thumbs", where i manually add thumbnails.
My question is whether it is better to manually add thumbnails or create thumbnails dynamically using "GD" or "ImageMagick".
Problems with adding Thumbnails manually
If i forget to add a thumbnail for photo the chain of the Gallery breaks
It is tedious to resize all the images for creating the thumbnails
So does using PHP ImageProcessing function add additional overhead in creating thumbnails or is the correct approach?
-- Updated for answering Queries
How you add images to the "gallery" folder?
Through Direct file upload
How those images and thumbnails are accessed?
I use glob to get list of files in respective folders
How do you (want to) map images to thumbnails?
For thumbnails i use imageName-thumb.jpg,
so they appear in the list in the same order as they are in main image folder
Use PHP to create thumbnails once and save them in a "thumbnails" directory with the same file names. Then use these now-ready-to-be-used thumbnails directly.
If your going with some Dynamic image processing tool then it will be little bit time consuming process, except that it will be a great technique.
If you are going with the manual process then:
Make thumbnail compulsory using some client side script
It depends on your image orientation
I create the thumbnails during upload and either save in a different directory with the same name or add th_ to the front of the filename.
You could also upload some code to generate all the thumbs again for the images you currently have in your gallery in case you have missed any.
You could also resize to different sizes at the same time, watermark or add other effects.
This is my gallery and I have a thumb and normal size; the thumb was sharpened on upload and the normal watermarked. Bother versions have a drop shadow and the resizing etc. was done with Imagemagick.
http://www.rubble.info/gallery/
If you check the file is there before displaying it and it is missing you can show a default image.
You can check out my website for lots of different things you can do with php and Imagemagick.
I highly recommend using GD with some sort of caching to keep the ones that don't change. There is, however, already a superb library for doing this. It's an absolute favourite of mine and gives you easy image compression and resizing just with a GET URL.
Try Smart Image Resizer by ShiftingPixel:
http://shiftingpixel.com/2008/03/03/smart-image-resizer/
You can, if you wish, use a website I made as a reference using its page source: http://www.eastwood-whelpton.co.uk/about/gallery.php
Nearly all images on this website use the Smart Image Resizer GD library.
I can also provide the PHP code I used if you wish for examples. This particular code automatically adds any images found in my gallery folder to this page.
As stated previously you need PHP 5 with GD support. If you have these then here is a very handy function to create thumbnails of a given dimensions and quality ($options) from every image in a given directory ($from_dir) and save them to another directory ($to_dir).
function make_thumbnails($from_dir,$to_dir, $options){
$files = scandir($from_dir);
$exclude = array('.','..','etc.');
foreach($files as $fi => $fv){
if(!in_array($fv,$exclude)){
$from_file = $from_dir.$fv;
$to_file = $target_dir.$fv;
list($img_width, $img_height,$img_type) = getimagesize($from_file);
$scale = min($options['max_width'] / $img_width,
$options['max_height'] / $img_height
);
$new_width = $img_width * $scale;
$new_height = $img_height * $scale;
$new_img = imagecreatetruecolor($new_width, $new_height);
$src_img = imagecreatefromjpeg($from_file);
$success = $src_img && imagecopyresampled(
$new_img,
$src_img,
0, 0, 0, 0,
$new_width,
$new_height,
$img_width,
$img_height
) && imagejpeg($new_img, $to_file, $options['quality']);
//Monitor results with $success - returns 1 or null
echo '<br />success:['.$success.']';
}
}
}
//Set options
$from_dir = ':/source/dir';
$to_dir = ':/destination/dir';
$options = array();
$options['max_width'] = 100;
$options['max_height'] = 100;
$options['quality'] = 100;
// Make thumbs...
make_thumbnails($from_dir,$to_dir,$options);
In PHP I'm trying to process an image, that is, I'm trying to make the surrounding color transparent in a jpg file. I'm using the GD library by the way.
I can directly output the image by converting it into a png using imagecreatefromjpeg and imagepng functions. But I can't find a way to make the specified color transparent. Also, some images have lighter gray artifacts around black graphics, created during saving. Is there any way I can include those as well?
I'm kind of lost. I found some answers to make a color transparent on an image but I don't know how to first convert the image without saving it into the server and then process it.
Any ideas?
EDIT: Here's my code so far. I managed to make a specified color transparent but I can't make it detect the surrounding one yet.
Most of the time images will be closed because they'l be logos or texts, saved in the allowed image formats. So I don't think I will have a major issue with gradients but it would be great if I could manage to manipulate transparency in the surrounding gradients, if any, such as drop shadows.
Is there also any way to detect if the png/gif image is already transparent? My code paints the transparent parts into black for those files now.
$file = 'images/18.jpg';
$specs = getimagesize($file);
if($specs[2] == 1) $img = imagecreatefromgif($file); //gif
elseif($specs[2] == 2) $img = imagecreatefromjpeg($file); //jpg
elseif($specs[2] == 3) $img = imagecreatefrompng($file); //png
else exit('unsupported file type!');
$newimg = imagecreatetruecolor(imagesx($img), imagesy($img));
// create a new image with the size of the old one
imagecopy($newimg,$img,0,0,0,0,imagesx($img),imagesy($img));
// copy the old one
imagedestroy($img);
// free the memory
$white = imagecolorallocate($newimg,255,255,255);
imagecolortransparent($newimg,$white);
// make white pixels transparent
header('Content-Type: image/png');
imagepng($newimg);
imagedestroy($newimg);
// and finally output the new image
You can set the transparent color with the imagecolortransparent function.
I have an image which i am going to be using as a background image and will be pulling some other images from the database that i want to show within this image. So if i am pulling only 1 image, i want the bottom part of the background image to close after the first image, if there are multiple images then i want it close after those images are shown. The problem with not using separate images is that the borders of the images have a design format and i cannot show them separately.
Take a look at this image . The design format of the right and left borders are more complicated than that to just crop them and use them. Any suggestions if there is any dynamic image resizing thing?
Yes there is. Look at the imageXXXX functions; the ones you are particularly interested in are imagecreate, imagecreatetruecolor, imagecreatefrompng, imagecopyresampled, imagecopyresized, and imagepng (assuming you're dealing with PNG images - there's similar load / save functions for jpeg, gif, and a few other formats).
You should try using the GD extension for PHP, especially have a look at imagecopyresized(). This allows you to do some basic image conversion and manipulation very easily.
A basic example that takes two GET parameters, resizes our myImage.jpg image and outputs it as a PNG image:
<?php
// width and height
$w = $_GET['w'];
$h = $_GET['h'];
// load image
$image = imagecreatefromjpeg('myImage.jpg');
// create a new image resource for storing the resized image
$resized = imagecreatetruecolor($w, $h);
// copy the image
imagecopyresized($resized, $image, 0, 0, 0, 0, $w, $h, imagesx($image), imagesy($image));
// output the image as PNG
header('Content-type: image/png');
imagepng($resized);
Have you tried PHPThumb? I used this class often and its pretty clean and lightweight. I used it here.
I have many (about 1000) images (printscreens), and I need to crop these images (all cropped images are in the same region in the full image).
How can I do this in php? Or maybe GIMP is supporting some macro scripts to do this?
Thanks in advance.
Except using GD, as Greg and n1313 proposed, you can also use ImageMagick for that, e.g. similar to Greg's solution, but with
$original_image = new Imagick($file->getRealPath());
$dest_image = $original_image->clone();
$dest_image->cropImage($width, $height, $x, $y);
$dest_image->writeImage('./resized/' . $file->getFilename());
You might check for exceptions when creating the new image (i.e. image cannot be opened) or returned false on cropImage and writeImage (image cannot be cropped or cannot be written).
You can do it in PHP using the GD image functions.
Your script might look something like this (not tested):
$it = new RecursiveDirectoryIterator('./screenshots');
foreach ($it as $file)
{
if (!preg_match('/\.jpe?g$/i', $file->getFilename()))
continue;
$src = imagecreatefromjpeg($file->getRealPath());
$dest = imagecreatetruecolor(1000, 1000);
imagecopyresampled($desc, $src, 0, 0, X_OFFSET, Y_OFFSET, 1000, 1000, WIDTH, HEIGHT);
imagejpeg($dest, './resized/' . $file->getFilename());
}
To do this with PHP you'll need a GD image manipulation library. Then you can use imagecopy() function to crop you image and many others GD functions to manipulate the image in whatever way you need.