Php – Resize image on upload php

file uploadimage processingPHP

I have a php script for image upload as below

<?php
$LibID = $_POST[name];
 define ("MAX_SIZE","10000");
 function getExtension($str) {
         $i = strrpos($str,".");
         if (!$i) { return ""; }
         $l = strlen($str) - $i;
         $ext = substr($str,$i+1,$l);
         return $ext;
 }
 $errors=0;
    $image=$_FILES['image']['name'];
    if ($image)
    {
        $filename = stripslashes($_FILES['image']['name']);
        $extension = getExtension($filename);
        $extension = strtolower($extension);
 if (($extension != "jpg") && ($extension != "jpeg"))
        {
            echo '<h1>Unknown extension!</h1>';
            $errors=1;
            exit();
        }
        else
        {
 $size=filesize($_FILES['image']['tmp_name']);
if ($size > MAX_SIZE*1024)
{
    echo '<h1>You have exceeded the size limit!</h1>';
    $errors=1;
    exit();
}
$image_name=$LibID.'.'.$extension;
$newname="uimages/".$image_name;
$copied = copy($_FILES['image']['tmp_name'], $newname);
if (!$copied)
{
    echo '<h1>image upload unsuccessfull!</h1>';
    $errors=1;
    exit();
}}}
?>

which uploads the image file to a folder "uimages" in the root. I have made changes in the html file for the compact display of the image by defining "max-height" and "max-width". But i want to resize the image file on upload. The image file may have a maximum width of 100px and maximum height of 150px. The image proportions must be constrained. That is, the image may be smaller than the above dimensions, but, it should not exceed the limit. How can I make this possible??

Thanks in advance 🙂

blasteralfred..

Best Answer

One cannot resize an image on upload; It must be on the server first.

For that, you can see Eric's answer using ImageMagick. If you're using the GD library, then you could use this.