Billede nedscaring (resizing)
(Dansk)
Dette er et kort script til at scalarer et billede ned, og lægger det op med et nyt navn ($newNamer). Nedenunder findes et eksempel vis af dens brug
Den anvendes således:
PHP ->
function resizePicture($filer, $path, $newNamer, $maxw, $maxh, $type=normal, $quality=100) {
global $file;
global $newName;
$file = $filer;
$newName = $newNamer;
$error = null;
//text to display if picture is uploaded successfully
$pictureUploaded = "
Woohoo, picture resized";
//file type error
$error1 = "
You may not resize that file type.";
//file size error.
$error2 = "
The maximum filesize has been exceeded (".$maxFileSize."
";
//file could not be uploaded
$error3 = "
File could not be uploaded.";
//file not found error...
$error4 = "
File not found";
//error if the type isn't supported
$error5 = "
The type $type is not supported.";
//set the approved filetypes
$imgTypes = array('jpg', 'png');
/* NO TOUCH */
//set file extension
$ext = array_pop(explode(".", strtolower($file)));
//switch for which type of image it is...
switch($ext) {
case 'jpeg':
case 'jpe':
case 'jpg':
$srcim = imagecreatefromjpeg ($file);
break;
case 'gif':
$srcim = imagecreatefromgif($file);
break;
case 'png':
$srcim = imagecreatefrompng($file);
break;
default:
$error = $error1;
}
//get the size of the orinal image
$ow = imagesx($srcim);
$oh = imagesy($srcim);
//resize the image
if($maxh == ""
{
$maxh = 1;
}
if($maxw == ""
{
$maxw = 1;
}
if($oh > $maxh) {
//set the scale
$wscale = ($maxw / $ow);
$hscale = ($maxh / $oh);
// set the scale - normal, height or width
if($type == "normal"
{
$scale = min( $hscale, $wscale);
} elseif ($type == "height"
{
$scale = $hscale;
} elseif($type == "width"
{
$scale = $wscale;
} else {
$error = $error5;
}
$nh = round($oh * $scale, 0);
$nw = round($ow * $scale, 0);
} else {
//image smaller then resize parameters... use original sizes
$nh = $oh;
$nw = $ow;
}
//file does not exist...
//now that it is resized we are aready to create color version of the image
//in the right size, and resample this
//with content
$dstim = imagecreatetruecolor($nw, $nh);
//resample
imagecopyresampled($dstim, $srcim, 0, 0, 0, 0, $nw, $nh, $ow, $oh);
//now create the image from which type the orinigal was
switch($ext) {
case 'jpeg':
case 'jpe':
case 'jpg':
imagejpeg($dstim, $path . $newNamer, $quality);
break;
case 'gif':
imagegif($dstim, $path . $newNamer);
break;
case 'png':
//create alpha for use with png clear bg's
imagesavealpha($dstim, true);
$trans_colour = imagecolorallocatealpha($dstim, 0, 0, 0, 127);
imagefill($dstim, 0, 0, $trans_colour);
$png_q = floor( abs($qaulity / 10 - 9.9));
imagepng($dstim, $path . $newNamer, $png_q);
break;
default:
$error = $error1;
}
//destroy the temporary images ^^
imagedestroy($dstim);
imagedestroy($srcim);
//set error if needed...
if($error == null) {
return $pictureUploaded ." and given the name: ". $newName;
} else {
return "
<b><span style='color: #FF0000;'>Warning:</span>".$error."</b>";
}
}
Den anvendes således:
PHP ->
$newName = null;
$file = null;
$filePath2 = "C:wwwJLRslackermikey.dkDesignshairdresseruploadedfiles--";
$filePath2 = str_replace("--", "", $filePath2);
echo resizePicture('http://www.slackermikey.dk/Designs/hairdresser/uploadedfiles/' . $fileName, $filePath2, $fileName, '250', '', 'width');
