1

my aim is to save uploaded image and set width+height to it. With the code below I can save the image, but cannot set width + height... Please have a look at the code between // and // to spot the mistakes. Thanks a lot.

$format = $_FILES["picture"]["type"];

$filename = $_FILES["picture"]["tmp_name"];

$destination = "upload/" .time(). $_FILES["picture"]["name"]; 

 ////////////////////////////////////////////

$_destination = width [400];
$_destination = height [460]; // this doesn't work... please help:) 

  //////////////////////////////////////////

  move_uploaded_file($filename, $destination); //moves image to "upload" folder

 $_SESSION['picture'] = $destination; //saves image path... 

 $_SESSION['format'] = $format; //saves image format
sofa_surfa
  • 43
  • 1
  • 7

2 Answers2

3

If on the server is only GD installed, you can use the following functions:

//helper function to resize an image based on input, output and size
function ResizeImage($input, $output, $mode, $w, $h = 0)
{
    switch(GetMimeType($input))
    {
        case "image/png":
            $img = ImageCreateFromPng($input);
            break;
        case "image/gif":
            $img = ImageCreateFromGif($input);
            break;
        case "image/jpeg":
        default:
            $img = ImageCreateFromJPEG ($input);
            break;
    }

    $image['sizeX'] = imagesx($img);
    $image['sizeY'] = imagesy($img);
    switch ($mode){
    case 1: //Quadratic Image
        $thumb = imagecreatetruecolor($w,$w);
        if($image['sizeX'] > $image['sizeY'])
        {
            imagecopyresampled($thumb, $img, 0,0, ($w / $image['sizeY'] * $image['sizeX'] / 2 - $w / 2),0, $w,$w, $image['sizeY'],$image['sizeY']);
        }
        else
        {
            imagecopyresampled($thumb, $img, 0,0, 0,($w / $image['sizeX'] * $image['sizeY'] / 2 - $w / 2), $w,$w, $image['sizeX'],$image['sizeX']);
        }
        break;

    case 2: //Biggest side given
        if($image['sizeX'] > $image['sizeY'])
        {
            $thumb = imagecreatetruecolor($w, $w / $image['sizeX'] * $image['sizeY']);
            imagecopyresampled($thumb, $img, 0,0, 0,0, imagesx($thumb),imagesy($thumb), $image['sizeX'],$image['sizeY']);
        }
        else
        {
            $thumb = imagecreatetruecolor($w / $image['sizeY'] * $image['sizeX'],$w);
            imagecopyresampled($thumb, $img, 0,0, 0,0, imagesx($thumb),imagesy($thumb), $image['sizeX'],$image['sizeY']);
        }
        break;
    case 3; //Both sides given (cropping)
        $thumb = imagecreatetruecolor($w,$h);
        if($h / $w > $image['sizeY'] / $image['sizeX'])
        {
            imagecopyresampled($thumb, $img, 0,0, ($image['sizeX']-$w / $h * $image['sizeY'])/2,0, $w,$h, $w / $h * $image['sizeY'],$image['sizeY']);
        }
        else
        {
            imagecopyresampled($thumb, $img, 0,0, 0,($image['sizeY']-$h / $w * $image['sizeX'])/2, $w,$h, $image['sizeX'],$h / $w * $image['sizeX']);
        }
        break;

    case 0:
        $thumb = imagecreatetruecolor($w,$w / $image['sizeX']*$image['sizeY']);
        imagecopyresampled($thumb, $img, 0,0, 0,0, $w,$w / $image['sizeX']*$image['sizeY'], $image['sizeX'],$image['sizeY']);
        break;
}        

    if(!file_exists($output)) imagejpeg($thumb, $output, 90);
}


//helper function to get the mime type of a file
function GetMimeType($file)
{
    $forbiddenChars = array('?', '*', ':', '|', ';', '<', '>');

    if(strlen(str_replace($forbiddenChars, '', $file)) < strlen($file))
        throw new \ArgumentException("Forbidden characters!");

    $file = escapeshellarg($file);

    ob_start();
    $type = system("file --mime-type -b ".$file);
    ob_clean();

    return $type;
}

and then use it in your code:

//$filename: input filename
//$destination: output filename
//$mode: 1, quadratic / 2, proportional / 3, cropped
//$width: output image width
//$height: optional output image height, only needed for mode 3
ResizeImage($filename, $destination, $mode, $width, $height);

EDIT: a little explanation about functions.

<?php

function DisplayText($text)
{
   echo $text;
}

function MultiplyNumbers($a, $b)
{
   return $a*$b;
}

$test = MultiplyNumbers(3, 6);
DisplayText($test);

-> output: 18

Raphael Müller
  • 2,180
  • 2
  • 15
  • 20
  • So do I have to change imagecopyresampled($thumb, $img, 0,0, ($image['sizeX']-$w / $h * $image['sizeY'])/2,0, $w,$h, $w / $h * $image['sizeY'],$image['sizeY']); and add 400 and 460 instead of 0,0? – sofa_surfa Dec 08 '14 at 17:01
  • no, just use `ResizeImage($filename, $destination, 400, 460);` and add the functions to your file. have a look at [user defined functions](http://php.net/manual/en/functions.user-defined.php) to get used to functions. if you want you can also go a step further to OOP. – Raphael Müller Dec 08 '14 at 17:06
  • In the first line of your code here: "function ResizeImage($input, $output, $w, $h { " Do I need to change $input to $destination in my case? – sofa_surfa Dec 08 '14 at 17:23
  • also I get a fatal error using "throw new \ArgumentException("Forbidden characters!"); " ... – sofa_surfa Dec 08 '14 at 17:24
  • most likely you have forbidden characters in your filename. – Raphael Müller Dec 08 '14 at 18:22
  • do I have to add the first code (in the first box) before $FILE and " ResizeImage($filename, $destination, 400, 460); " after the $FILE ? – sofa_surfa Dec 08 '14 at 20:37
  • its a good practice to have the functions defined before you use them. and if you still get some errors, try without the switch case and use just the code for jpeg or png. – Raphael Müller Dec 08 '14 at 22:17
  • I got this to work, but the image is cropped. Is there a way of keeping the image not cropped while having the same width+height? – sofa_surfa Dec 08 '14 at 23:49
  • what do you mean with the same width+height? keep the proportions of the old one? or do you want black bars on the side? (if you provide booth sides which doesn't have the same proportions than the original. please see my updated answer for the case where you want to keep the proportions, i.e. the biggest side is given. – Raphael Müller Dec 09 '14 at 07:11
  • So does this code changes width+height with the same proportions ? – sofa_surfa Dec 09 '14 at 08:33
  • the updated code computes the new width if you choose mode 1 or 2 i.e. if your image has the proportion 2:3 and you supply 300 in mode 2, your small side will be 200. if you provide 400 in mode 1, the other side is also 400 and cropped. in mode 3 you set both sides, and everything gets cropped i.e. if your file is 3:2 (portrait) and you specify 200:300 your cropped area is landscape with a width of 300. – Raphael Müller Dec 09 '14 at 08:50
  • So do I need to change "//$mode: 1, quadratic / 2, proportional / 3, cropped" to choose the mode? or the top line "function ResizeImage($input, $output, $mode, $w, $h = 0)" and change 0 ? – sofa_surfa Dec 09 '14 at 09:19
  • where you call your function use as example `ResizeImage("infile.jpg", "outfile.jpg", 2, 300);` -> this sets the mode to 2 and the biggest side of your image to 300 – Raphael Müller Dec 09 '14 at 09:32
  • Ok that worked with only bigger side, but is there a way of changing both the width and the height? – sofa_surfa Dec 09 '14 at 20:14
  • If you use mode 3, you can specify both sides, (everything that is bigger in proportion gets cropped) or do you want black bars in your image? – Raphael Müller Dec 10 '14 at 08:08
0

Imagick will perfectly suit your needs: http://php.net/manual/en/imagick.setsize.php

Using setSize before reading an image file tells ImageMagick to resize the image immediately on load - this can give a substantial increase in performance time and save memory and disk resources for large images:

<?php

$image = new Imagick();
$image->setSize(800,600);
$image->readImage($file);

?>
Matheno
  • 4,112
  • 6
  • 36
  • 53