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
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
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);
?>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.