Menu
I usually use a resize script for my JPEG resizing. However, I now want to write or use a script that can take the middle section of a JPEG.
For example, if want an area of 100×100 pixels in an image to be exactly the right size how do I do this?
I noticed this is used on sites like flickr.
Cheers
[code=php]function resize_test($source, $destination = null, $w = 200, $h = 170, $quality = 100)
{
$details = @getimagesize($source) or die("I cannot open $source");
$type = preg_replace('@^.+(?<=/)(.+)$@', '$1', $details['mime']);
eval('$source = imagecreatefrom'.$type.'($source);');
$h = round(($w / $details[0]) * $details[1]);
if(imageistruecolor($source))
{
$slate = @imagecreatetruecolor($w, $h) or die('Invalid thumbnail dimmensions');
imageAlphaBlending($slate, false);
imageSaveAlpha($slate, true);
}
else
{
$slate = @imagecreate($w, $h) or die('Invalid thumbnail dimensions');
if(false !== ($trans = @imagecolorsforindex($source, imagecolortransparent($source))))
{
$trans = ImageColorAllocate($slate, $trans['red'], $trans['green'], $trans['blue']);
imagefilledrectangle($slate, 0, 0, $w - 1, $h - 1, $trans);
imagecolortransparent($slate, $trans);
}
}
imagecopyresampled($slate, $source, 0, 0, 0, 0, $w, $h, $details[0], $details[1]);
$destination or header('Content-Type: '.$details['mime']);
eval('@image'.$type.'($slate'.(($type=='jpeg')?',$destination,$quality':($destination?',$destination ':'')).');');
imagedestroy($source);
imagedestroy($slate);
$destination or die;
} [/code]
[code=php]function CropAbstractCentered($source, $destination = null, $w = 100, $h = 100, $quality = 100)
{
$source_image = @imagecreatefromjpeg($source)
or die($source.' is not a valid image');
$sw = imagesx($source_image);
$sh = imagesy($source_image);
$x = ($sw-$w)/2;
$y = ($sh-$h)/2;
$slate = @imagecreatetruecolor($w, $h) or die('Invalid thumbnail dimensions');
imagecopy($slate, $source_image, 0, 0, $x, $y, $w, $h);
$destination or header('Content-Type: image/jpeg');
@imagejpeg($slate, $destination, 100) or die('Directory permission problem');
ImageDestroy($source_image);
ImageDestroy($slate);
$destination or exit;
return TRUE;
}[/code]
I wrote that!
[code=php]function CropAbstractCentered($source, $destination = null, $w = 100, $h = 100, $quality = 100)
[/QUOTE]
{
$source_image = @imagecreatefromjpeg($source)
or die($source.' is not a valid image');
$sw = imagesx($source_image);
$sh = imagesy($source_image);
$x = ($sw-$w)/2;
$y = ($sh-$h)/2;
$slate = @imagecreatetruecolor($w, $h) or die('Invalid thumbnail dimensions');
imagecopy($slate, $source_image, 0, 0, $x, $y, $w, $h);
$destination or header('Content-Type: image/jpeg');
@imagejpeg($slate, $destination, 100) or die('Directory permission problem');
ImageDestroy($source_image);
ImageDestroy($slate);
$destination or exit;
return TRUE;
}[/code]
[code=php]eval('$source = imagecreatefrom'.$type.'($source);');[/code]
[/quote]
[code=php]$func = 'imagecreatefrom' . $type;
$source = $func($source);[/code]
Just a small question:Eval is a security loophole. If $source contained parseable executable code it would be run. Since $source comes from outside the function it is impossible to know what it contains. The eval lower down has the same problem with $destination. Eval probably takes a bit more time too because the code goes back to the compiler.
Isn't it better this way?[/QUOTE]
[code=php]function resize_thumbnail($source, $destination = null, $w = 150, $h = 120, $quality = 95)
{
$details = @getimagesize($source) or die("I cannot open $source");
$type = preg_replace('@^.+(?<=/)(.+)$@', '$1', $details['mime']);
eval('$source = imagecreatefrom'.$type.'($source);');
if ($details[0] != $details[1])
{
if($details[0] < $details[1])
{ $w = round(($h / $details[1]) * $details[0]); }
else
{ $h = round(($w / $details[0]) * $details[1]); }
}
if(imageistruecolor($source))
{
$slate = @imagecreatetruecolor($w, $h) or die('Invalid thumbnail dimmensions');
imageAlphaBlending($slate, false);
imageSaveAlpha($slate, true);
}
else
{
$slate = @imagecreate($w, $h) or die('Invalid thumbnail dimensions');
if(false !== ($trans = @imagecolorsforindex($source, imagecolortransparent($source))))
{
$trans = ImageColorAllocate($slate, $trans['red'], $trans['green'], $trans['blue']);
imagefilledrectangle($slate, 0, 0, $w - 1, $h - 1, $trans);
imagecolortransparent($slate, $trans);
}
}
imagecopyresampled($slate, $source, 0, 0, 0, 0, $w, $h, $details[0], $details[1]);
$destination or header('Content-Type: '.$details['mime']);
eval('@image'.$type.'($slate'.(($type=='jpeg')?',$destination,$quality':($destination?',$destination ':'')).');');
imagedestroy($source);
imagedestroy($slate);
$destination or die;
} [/code]
[code=php]function ResizeSemiAbstractCentered($source, $destination = null, $w = 120, $h = 90, $quality = 100)
{
$source_image = @imagecreatefromjpeg($source)
or die($source.' is not a valid image');
$sw = imagesx($source_image);
$sh = imagesy($source_image);
$ar = $sw/$sh;
$tar = $w/$h;
if($ar >= $tar)
{
$x1 = round(($sw - ($sw * ($tar/$ar)))/2);
$x2 = round($sw * ($tar/$ar));
$y1 = 0;
$y2 = $sh;
}
else
{
$x1 = 0;
$y1 = round(($sh - ($sw/$tar))/2);
$x2 = $sw;
$y2 = round($sw/$tar);
}
$slate = @imagecreatetruecolor($w, $h) or die('Invalid thumbnail dimmensions');
imagecopyresampled($slate, $source_image, 0, 0, $x1, $y1, $w, $h, $x2, $y2);
$destination or header('Content-Type: image/jpeg');
@imagejpeg($slate, $destination, $quality) or die('Directory permission problem');
ImageDestroy($slate);
ImageDestroy($source_image);
$destination or exit;
return true;
}[/code]
0.1.9 — BETA 6.17