开发者

Upload image, create its thumb then save

Let's say I uploaded an image. I can get its temp directory, then save it with move_uploaded_file() but what if I wanted to also create a thumb and save both in some folder?

I know how to save the uploaded image, but I don't know how to start manipulating an ima开发者_如何学Cge and save it after creating a thumb.


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


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


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


Use Image magick. check previous posts on stack overflow Imagemagick thumbnail generation with php - using -crop PHP: Creating cropped thumbnails of images, problems 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);
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜