/    Sign up×
Community /Pin to ProfileBookmark

Taking a section of a JPEG

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

to post a comment
PHP

11 Comments(s)

Copy linkTweet thisAlerts:
@tfk11Mar 13.2008 — Lookup the imagecopyresampled function.
Copy linkTweet thisAlerts:
@bokehMar 13.2008 — If you are just doing a crop use imagecopy(). Resampling is only necessary if the image is being scaled.
Copy linkTweet thisAlerts:
@sanchez_1960authorMar 26.2008 — Thanks for the replies. Here is the code i'm using, which I believe was written by somebody here:

[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]


How would I modify this to take the center section of a area, say 100x100 but the center section?
Copy linkTweet thisAlerts:
@bokehMar 26.2008 — I wrote that!

[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]
Copy linkTweet thisAlerts:
@sanchez_1960authorMar 26.2008 — I wrote that!

[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]
[/QUOTE]


Thanks so much!
Copy linkTweet thisAlerts:
@ZnupiMar 26.2008 — Just a small question:
[code=php]eval('$source = imagecreatefrom'.$type.'($source);');[/code][/quote]
Isn't it better this way?
[code=php]$func = 'imagecreatefrom' . $type;
$source = $func($source);[/code]

I'm just asking, I want to know which way is better.
Copy linkTweet thisAlerts:
@bokehMar 27.2008 — Just a small question:

Isn't it better this way?[/QUOTE]
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.

I think most people would replace this piece of code with a switch statement. The switch is probably best as it negates the need to use preg (or similar) to fish out the type.
Copy linkTweet thisAlerts:
@sanchez_1960authorMar 27.2008 — Another question for you.

This version you wrote originally works out the average and never goes over 150 pixels wide. The trouble is i want to also implement a maximum of 120 in height. It works if the aspect ratio is way out for example 160 x 300 works perfect, but if its 160 x 150 it always ends up too deep for me.

So basically I want to modify the following so it never goes over 150 pixels wide and never over 120 pixels deep.

[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]


I seriously do appreciate your help. You've really helped me out using this on my site.
Copy linkTweet thisAlerts:
@bokehMar 27.2008 — Well you could make the thumbnail fit your dimensions exactly and if the source image has a aspect ratio different from that of the thumbnail just trim off the excess. For example:[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]
Copy linkTweet thisAlerts:
@sanchez_1960authorMar 28.2008 — I love you man! ?
Copy linkTweet thisAlerts:
@bokehMar 29.2008 — I did the same with my gallery script (code here) but instead of taking the very centre of the image I took the top section which means in portraits the thumbnail includes the head.
×

Success!

Help @sanchez_1960 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...