Resize image on upload php
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-h开发者_Python百科eight" 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..
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.
You can resize an image using PHPs native GD library. Here is a link to a function I wrote for resizing an image to any arbitrary size. It has options for letterboxing or cropping to fit to make it fit the new aspect ratio, and a pretty good explaination.
http://www.spotlesswebdesign.com/blog.php?id=1
you can use 2 functions to resize images after uploads
<?php
function thumbnail($inputFileName, $maxSize = 100) {
$info = getimagesize($inputFileName);
$type = isset($info['type']) ? $info['type'] : $info[2];
if (!(imagetypes() & $type)) {
return false;
}
$width = isset($info['width']) ? $info['width'] : $info[0];
$height = isset($info['height']) ? $info['height'] : $info[1];
// Calculate aspect ratio
$wRatio = $maxSize / $width;
$hRatio = $maxSize / $height;
// Using imagecreatefromstring will automatically detect the file type
$sourceImage = imagecreatefromstring(file_get_contents($inputFileName));
// Calculate a proportional width and height no larger than the max size.
if (($width <= $maxSize) && ($height <= $maxSize)) {
// Input is smaller than thumbnail, do nothing
return $sourceImage;
} elseif (($wRatio * $height) < $maxSize) {
// Image is horizontal
$tHeight = ceil($wRatio * $height);
$tWidth = $maxSize;
} else {
// Image is vertical
$tWidth = ceil($hRatio * $width);
$tHeight = $maxSize;
}
$thumb = imagecreatetruecolor($tWidth, $tHeight);
if ($sourceImage === false) {
// Could not load image
return false;
}
// Copy resampled makes a smooth thumbnail
imagecopyresampled($thumb, $sourceImage, 0, 0, 0, 0, $tWidth, $tHeight, $width, $height);
imagedestroy($sourceImage);
return $thumb;
}
function imageToFile($im, $fileName, $quality = 80) {
if (!$im || file_exists($fileName)) {
return false;
}
$ext = strtolower(substr($fileName, strrpos($fileName, '.')));
switch ($ext) {
case '.gif':
imagegif($im, $fileName);
break;
case '.jpg':
case '.jpeg':
imagejpeg($im, $fileName, $quality);
break;
case '.png':
imagepng($im, $fileName);
break;
case '.bmp':
imagewbmp($im, $fileName);
break;
default:
return false;
}
return true;
}
?>
make call like this : $image = thumbnail($uploadedFile, 300); imageToFile($image, $folder); // if you want to save the thumbnail
See this answer, I like myself using imagemagick
精彩评论