List Info

Thread: note 61864 added to function.imagecopyresized




note 61864 added to function.imagecopyresized
user name
2006-02-14 07:06:42
I wrote a new image cropper, because when i wanted to crop
an image to different aspect, it simply stretched , to fit
with new aspect! here is the solution , with this function
you can easily crop any image to any size, without changing
aspect.

function flexible_image_cropper($source_location,
$desc_location=null,$crop_size_w=null,$crop_size_h=null)
{
	list($src_w, $src_h) = getimagesize($source_location);

	$image_source = imagecreatefromjpeg($source_location);
	$image_desc =
imagecreatetruecolor($crop_size_w,$crop_size_h);

/*
	if ($crop_size_w>$crop_size_h) {$my_crop_size_w=null;}
		elseif ($crop_size_h>$crop_size_w)
			{$my_crop_size_h=null;
	
	if (is_null($my_crop_size_h) and !is_null($my_crop_size_w))
		{ $my_crop_size_h=$src_h*$my_crop_size_w/$src_w; } 
	elseif (!is_null($my_crop_size_h))
		{ $my_crop_size_w=$src_w*$my_crop_size_h/$src_h; }
*/

	if ($src_w<$src_h)
{$my_crop_size_h=$src_h*$crop_size_w/$src_w;} else
{$my_crop_size_h=$crop_size_h;}
	if ($src_h<$src_w)
{$my_crop_size_w=$src_w*$crop_size_h/$src_h;} else
{$my_crop_size_w=$crop_size_w;}
//		echo "($my_crop_size_w-$my_crop_size_h)";
	if ($my_crop_size_w>$crop_size_w)
{$additional_x=round(($my_crop_size_w-$crop_size_w)/2);}
else {$additional_x=0;}
	if ($my_crop_size_h>$crop_size_h)
{$additional_y=round(($my_crop_size_h-$crop_size_h)/2);}
else {$additional_y=0;}

	
	
	$off_x=round($src_w/2)-round($my_crop_size_w/2);
	$off_y=round($src_h/2)-round($my_crop_size_h/2)+$additional
_y;
	$off_w=(round($src_w/2)+round($my_crop_size_w/2))-$off_x;
	$off_h=(round($src_h/2)+round($my_crop_size_h/2))-$off_y;
	
	imagecopyresampled($image_desc, $image_source,0, 0, $off_x,
$off_y, $my_crop_size_w, $my_crop_size_h, $off_w, $off_h);

	if (!is_null($desc_location))
		imagejpeg($image_desc, $desc_location);
	else
		imagejpeg($image_desc);
}

SAMPLE :

$source_file='gallery_files/IMG_9736.jpg';
$desc_file='gallery_files/test.jpg';
flexible_image_cropper($source_file,$desc_file,200,200);
$source = imagecreatefromjpeg($desc_file);
----
Server IP: 64.71.164.2
Probable Submitter: 84.241.17.59
----
Manual Page -- http://www.php.net/manual/en/function.imagecopyresized
.php
Edit        -- http://master.php.net/manage/user-notes.php?action=
edit+61864
Delete: added to the manual -- htt
p://master.php.net/manage/user-notes.php?action=delete+61864
&report=yes&reason=added+to+the+manual
Delete: bad code            -- http://master.
php.net/manage/user-notes.php?action=delete+61864&report
=yes&reason=bad+code
Delete: spam                -- http://master.php.
net/manage/user-notes.php?action=delete+61864&report=yes
&reason=spam
Delete: useless             -- http://master.p
hp.net/manage/user-notes.php?action=delete+61864&report=
yes&reason=useless
Delete: non-english         -- http://mast
er.php.net/manage/user-notes.php?action=delete+61864&rep
ort=yes&reason=non-english
Delete: already in docs     -- http://
master.php.net/manage/user-notes.php?action=delete+61864&
;report=yes&reason=already+in+docs
Delete: other reasons       -- http://master.php.net/manage/user-
notes.php?action=delete+61864&report=yes
Reject      -- http://master.php.net/manage/user-
notes.php?action=reject+61864&report=yes
Search      -- http://ma
ster.php.net/manage/user-notes.php

-- 
PHP Notes Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub
.php

note 61864 deleted from function.imagecopyresized by mazzanet
user name
2006-02-14 09:55:12
Note Submitter: sinasalek at yahoo dot com 

----

I wrote a new image cropper, because when i wanted to crop
an image to different aspect, it simply stretched , to fit
with new aspect! here is the solution , with this function
you can easily crop any image to any size, without changing
aspect.

function flexible_image_cropper($source_location,
$desc_location=null,$crop_size_w=null,$crop_size_h=null)
{
	list($src_w, $src_h) = getimagesize($source_location);

	$image_source = imagecreatefromjpeg($source_location);
	$image_desc =
imagecreatetruecolor($crop_size_w,$crop_size_h);

/*
	if ($crop_size_w>$crop_size_h) {$my_crop_size_w=null;}
		elseif ($crop_size_h>$crop_size_w)
			{$my_crop_size_h=null;
	
	if (is_null($my_crop_size_h) and !is_null($my_crop_size_w))
		{ $my_crop_size_h=$src_h*$my_crop_size_w/$src_w; } 
	elseif (!is_null($my_crop_size_h))
		{ $my_crop_size_w=$src_w*$my_crop_size_h/$src_h; }
*/

	if ($src_w<$src_h)
{$my_crop_size_h=$src_h*$crop_size_w/$src_w;} else
{$my_crop_size_h=$crop_size_h;}
	if ($src_h<$src_w)
{$my_crop_size_w=$src_w*$crop_size_h/$src_h;} else
{$my_crop_size_w=$crop_size_w;}
//		echo "($my_crop_size_w-$my_crop_size_h)";
	if ($my_crop_size_w>$crop_size_w)
{$additional_x=round(($my_crop_size_w-$crop_size_w)/2);}
else {$additional_x=0;}
	if ($my_crop_size_h>$crop_size_h)
{$additional_y=round(($my_crop_size_h-$crop_size_h)/2);}
else {$additional_y=0;}

	
	
	$off_x=round($src_w/2)-round($my_crop_size_w/2);
	$off_y=round($src_h/2)-round($my_crop_size_h/2)+$additional
_y;
	$off_w=(round($src_w/2)+round($my_crop_size_w/2))-$off_x;
	$off_h=(round($src_h/2)+round($my_crop_size_h/2))-$off_y;
	
	imagecopyresampled($image_desc, $image_source,0, 0, $off_x,
$off_y, $my_crop_size_w, $my_crop_size_h, $off_w, $off_h);

	if (!is_null($desc_location))
		imagejpeg($image_desc, $desc_location);
	else
		imagejpeg($image_desc);
}

SAMPLE :

$source_file='gallery_files/IMG_9736.jpg';
$desc_file='gallery_files/test.jpg';
flexible_image_cropper($source_file,$desc_file,200,200);
$source = imagecreatefromjpeg($desc_file);

-- 
PHP Notes Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub
.php

[1-2]

about | contact  Other archives ( Real Estate discussion Medical topics )