Resize image on server

后端 未结 3 1311
执笔经年
执笔经年 2020-12-20 10:10

I made a file that is in charge of uploading images, this images are then moved to a folder in the server. I think I can\'t resize the image directly in the $_FILES array so

相关标签:
3条回答
  • 2020-12-20 10:57

    Move the uploaded file to a tmp directory (use tmp_name in $_FILES for original location), read it in using gd, resize then save it to the final directory.

    http://php.net/manual/en/function.move-uploaded-file.php http://us3.php.net/manual/en/function.imagecreate.php http://us3.php.net/manual/en/function.imagecopyresized.php

    0 讨论(0)
  • 2020-12-20 11:07

    PHP comes built-in with the GD library.

    There are many functions available for manipulating images however there's no need to re-invent the wheel.

    Check out this gist for a simple image manipulation class - https://gist.github.com/880506

    Here's an example usage...

    $im = new ImageManipulator($_FILES['field_name']['tmp_name']);
    $im->resample(640, 480); // resize to 640x480
    $im->save('/path/to/destination/image.jpg', IMAGETYPE_JPEG);
    
    0 讨论(0)
  • 2020-12-20 11:12

    I wasn't creating a file to the server's dir so this is what I did move_uploaded_file($_FILES[$str]['tmp_name'], $target); scale_image($target,$target);

    Now the function scale_image()

    function scale_image($image,$target)
    {
      if(!empty($image)) //the image to be uploaded is a JPG I already checked this
      {
         $source_image = imagecreatefromjpeg($image);
         $source_imagex = imagesx($source_image);
         $source_imagey = imagesy($source_image);
    
         $dest_imagex = 300;
         $dest_imagey = 200;
    
         $image2 = imagecreatetruecolor($dest_imagex, $dest_imagey);
         imagecopyresampled($image2, $source_image, 0, 0, 0, 0,
         $dest_imagex, $dest_imagey, $source_imagex, $source_imagey);
    
         imagejpeg($image2, $target, 100);
    
      }
    }
    

    Thank you all very much, the resource you gave me helped me to create this function.

    0 讨论(0)
提交回复
热议问题