Upload an image, create a thumb, then save

Say I uploaded an image. I can get its temp directory and then save it using move_uploaded_file(), but what if I want to also create a thumb and save both in some folder?

I know how to save the downloaded image, but I don’t know how to start manipulating the image and save it after creating the thumb.

+3
source share
4 answers

I always use the Verot PHP Upload class and have always been successful. This PHP class is really easy to implement and can manipulate the image in any way. It can also save the image in the specified folder.

You can download it from here.

To view demos and simple documentation for the download class, visit http://www.verot.net/php_class_upload_samples.htm?PHPSESSID=5375147e959625e56e0127f3458a6385

Below is a simple example that 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');
+1
source

The GD library makes this super easy.

, , . , : http://www.phptoys.com/e107_plugins/content/content.php?content.46

+3

php gd imagemagick. gd ( ):

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

. Imagemagick php - -crop PHP: , 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

Source: https://habr.com/ru/post/1755381/


All Articles