views:

60

answers:

4

Let's say I uploaded and image. I can get its tmp dir then save it with move_uploaded_file() but what if I wanted to also create a thumb and save bothin some folder?

I know how to save the uploaded image, but I don't know how to start manipulating an image and save it after creating a thumb

A: 

you need either php gd or imagemagick. here is a quick example of resizing using gd(from the manual):

<?php
// File and new size
$filename = 'test.jpg';
$percent = 0.5;

// Content type
header('Content-type: image/jpeg');

// Get new sizes
list($width, $height) = getimagesize($filename);
$newwidth = $width * $percent;
$newheight = $height * $percent;

// Load
$thumb = imagecreatetruecolor($newwidth, $newheight);
$source = imagecreatefromjpeg($filename);

// Resize
imagecopyresized($thumb, $source, 0, 0, 0, 0, $newwidth, $newheight, $width, $height);

// Output
imagejpeg($thumb, 'thumbs/thumb1.jpg');
?>
kgb
How do I save the image in some folder instead of displaying it?
Rodrigo Alves
the second parameter of the imagejpeg function is the filename. i've updated the example. also, check out the man page: http://php.net/manual/en/function.imagejpeg.php
kgb
+3  A: 

The GD library makes this super-easy.

You will want to calculate the dimensions of your original image so that you preserve your aspect ratio, then it's just a case of resampling the image in a smaller size. Here is a good tutorial that clearly explains everything: http://www.phptoys.com/e107_plugins/content/content.php?content.46

hollsk
A: 

Use Image magick. check previous posts on stack overflow http://stackoverflow.com/questions/1679092/imagemagick-thumbnail-generation-with-php-using-crop http://stackoverflow.com/questions/2670386/php-creating-cropped-thumbnails-of-images-problems http://stackoverflow.com/questions/2039144/image-processing-class-in-php http://www.imagemagick.org/

define('THUMB_WIDTH', 60);
define('THUMB_HEIGHT', 80);
define('MAGICK_PATH','/usr/local/bin/');

function makeThumbnail($in, $out) {
    $width = THUMB_WIDTH;
    $height = THUMB_HEIGHT;
    list($w,$h) = getimagesize($in);

    $thumbRatio = $width/$height;
    $inRatio = $w/$h;
    $isLandscape = $inRatio > $thumbRatio;

    $size = ($isLandscape ? '1000x'.$height : $width.'x1000');
    $xoff = ($isLandscape ? floor((($inRatio*$height)-$width)/2) : 0);
    $command = MAGICK_PATH."convert $in -resize $size -crop {$width}x{$height}+{$xoff}+0 ".
        "-colorspace RGB -strip -quality 90 $out";

    exec($command);
}
Ghost
+1  A: 

I always use Verot PHP Upload class and always had success with it. This PHP class is really simple to implement and can manipulate a image any way you want. It can save the image also in the specified folder.

You can download it from here

To view demos and easy documentation of the Upload class visit http://www.verot.net/php_class_upload_samples.htm?PHPSESSID=5375147e959625e56e0127f3458a6385

Below is a simple sample I got from the website

//How to use it?
//Create a simple HTML file, with a form such as:

 <form enctype="multipart/form-data" method="post" action="upload.php">
   <input type="file" size="32" name="image_field" value="">
   <input type="submit" name="Submit" value="upload">
 </form>

//Create a file called upload.php:

  $handle = new upload($_FILES['image_field']);
  if ($handle->uploaded) {
      $handle->file_new_name_body   = 'image_resized';
      $handle->image_resize         = true;
      $handle->image_x              = 100;
      $handle->image_ratio_y        = true;
      $handle->process('/home/user/files/');
      if ($handle->processed) {
          echo 'image resized';
          $handle->clean();
      } else {
          echo 'error : ' . $handle->error;
      }
  }

//How to process local files?
//Use the class as following, the rest being the same as above:

  $handle = new upload('/home/user/myfile.jpg');
Roland