0

I've been searching for several hours to find a way to resize an image to a fixed size before uploading it to server, but no solution seems suitable.

Is there a simple way to select an image through form input, then resize it and finally save it to a folder?

What I have now is:

<?php

$mypic = $_FILES['upload']['name'];
$temp = $_FILES['upload']['tmp_name'];
$type = $_FILES['upload']['type'];
$velikostobrazku = getimagesize($temp);

if(($type=="image/jpeg") || ($type=="image/jpg") || ($type=="image/png"))
{
$img_puvodni = move_uploaded_file($temp, "images/$mypic");
echo "<img src='images/$mypic'><p>";
echo $velikostobrazku[0],$velikostobrazku[1];
}
?>

And index.php's form:

<form method="post" enctype="multipart/form-data" action="upload_file.php">
<input type="file" name="upload" />
<input type="submit" value="Nahrát" />
<input type="hidden" name="MAX_FILE_SIZE" value="100000" />
</form>

Thank you in advance, Jan

Jan Mares
  • 57
  • 9

2 Answers2

0

You can try to use canvas in HTML5:

http://hacks.mozilla.org/2011/01/how-to-develop-a-html5-image-uploader/.

Here is example: HTML5 Pre-resize images before uploading

Community
  • 1
  • 1
Piotr Olaszewski
  • 6,017
  • 5
  • 38
  • 65
0

For PHP resize task, my go to is white hat and their resize fucntion because it keeps the transparency for PNG et JPG using their SimpleImage class

Edit: Here's a usage example:

if( isset($_FILES['your_input_name']) ) {
    include('SimpleImage.php');
    $image->load($_FILES["your_input_name"]["tmp_name"]);
    $image->resize(250,400); //Your fixed width/height
    $image->save('picture2.jpg'); // Your target location. You could use the name or generate it.
                                  // Personally I like to generate the name because space,     
                                  // accent, special character can mess up the upload
}
Michael Villeneuve
  • 3,933
  • 4
  • 26
  • 40
  • Thank you Michael, but the problem for my use is that I do not intend to load an image from file, but from an integer, to which the image was set from a form input throught POST method. – Jan Mares May 25 '14 at 19:53
  • I really don't understand. I'm reading your question again and it doesn't match what you're telling me. I have a hard time understanding what you're trying to acheive. – Michael Villeneuve May 25 '14 at 20:07
  • Thank you, Michael. The thing is that I need the source image to be dynamic - the example that you suggest expects the file to be already uploaded on the server. This would not be a problem generally, but how can I select a certain image every time a user uploads an image? – Jan Mares May 25 '14 at 20:39
  • The example was generic. I updated it for your needs. – Michael Villeneuve May 25 '14 at 21:07