note 106431 deleted from function.imagecopyresized by danbrown
| From: | danbrown@php.net | Date: | Tue, 15 Nov 2011 16:43:33 +0000 |
| Subject: | note 106431 deleted from function.imagecopyresized by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-184494@lists.php.net to get a copy of this message | ||
Note Submitter: iffy from Lagos
----
<?php
/**
I made a little adjustment to "it at chmzap dot ru" code after using it and realizing that
the resizing ratio was a bit off. The only change was in the logic where i replaced
if($gis[0] <= $size)
{
$av = $x;
$ah = $y;
}
else
{
$yc = $y*1.3333333;
$d = $x>$yc?$x:$yc;
$c = $d>$size ? $size/$d : $size;
$av = $x*$c; //вÑÑоÑа иÑÑ
одной
каÑÑинки
$ah = $y*$c; //длина иÑÑ
одной
каÑÑинки
}
with
$imgratio = $gis[0]/$gis[1];
if($imgratio>1)
{
$av=$size;
$ah=$size/$imgratio;
}
else
{
$ah=$size;
$av=$size*$imgratio;
}
We get the ratio of both sides of the image and determine which is larger, the larger side is then
adjusted to the image size set by the user, while the smaller side is adjusted to a proportionate
size based on the image ratio.
*/
/**
* Make thumbs from JPEG, PNG, GIF source file
*
* $tmpname = $_FILES['source']['tmp_name'];
* $size - max width size
* $save_dir - destination folder
* $save_name - tnumb new name
*
* Author: LEDok - http://www.citadelavto.ru/
*/
function img_resize( $tmpname, $size, $save_dir, $save_name )
{
$save_dir .= ( substr($save_dir,-1) != "/") ? "/" : "";
$gis = GetImageSize($tmpname);
$type = $gis[2];
switch($type)
{
case "1": $imorig = imagecreatefromgif($tmpname); break;
case "2": $imorig = imagecreatefromjpeg($tmpname);break;
case "3": $imorig = imagecreatefrompng($tmpname); break;
default: $imorig = imagecreatefromjpeg($tmpname);
}
$x = imageSX($imorig);
$y = imageSY($imorig);
$imgratio = $gis[0]/$gis[1];
if($imgratio>1)
{
$av=$size;
$ah=$size/$imgratio;
}
else
{
$ah=$size;
$av=$size*$imgratio;
}
$im = imagecreate($av, $ah);
$im = imagecreatetruecolor($av,$ah);
if (imagecopyresampled($im,$imorig , 0,0,0,0,$av,$ah,$x,$y))
if (imagejpeg($im, $save_dir.$save_name))
return true;
else
return false;
}
?>
And now how using this function fast
<?php
if ($_POST[pic])
{
$tmpname = $_FILES['pic']['tmp_name'];
@img_resize( $tmpname , 600 , "../album" , "album_".$id.".jpg");
@img_resize( $tmpname , 120 , "../album" , "album_".$id."_small.jpg");
@img_resize( $tmpname , 60 , "../album" ,
"album_".$id."verysmall.jpg");
}
else
echo "No Images uploaded via POST";
?>