View previous topic :: View next topic |
Author |
Message |
amnuts Site Admin

Joined: 01 Sep 2002 Posts: 662 Location: East Sussex, England
|
Posted: Mon Dec 01, 2003 5:13 am Post subject: User submitted class; 'imgRedim' |
|
|
Hi,
This little class that extends the crop canvas class was just posted up by board member 'db'. I thought others might appreciate it too, so I'm reposting in a more noticable area.
If others have any other extensions they'd like to share, then post them up!
Regards,
Andy
db wrote: | With the redimToSize function you can resize the picture to certain width and height, keeping the aspect ratio, and if you set the variable $crop = TRUE, your image is then cropped to the size you asked (if the resizing didn't reach the width and height). |
PHP: | <?php
class imgRedim extends canvasCrop
{
function imgRedim($debug = FALSE)
{
$this->canvasCrop($debug);
}
function redimToSize($x, $y, $crop = FALSE)
{
return ($this->_reSize($x, $y, $crop, 'redimToSize'));
}
function _reSize($nx, $ny, $crop = FALSE, $function)
{
if ($this->_imgOrig == NULL)
{
$this->_debug($function, 'The original image has not been loaded.');
return FALSE;
}
if (($nx <= 0) || ($ny <= 0))
{
$this->_debug($function, 'The image could not be resized because the size given is not valid.');
return FALSE;
}
$ox = @ImageSX($this->_imgOrig);
$oy = @ImageSY($this->_imgOrig);
$nnx = $nx;
$nny = $ny;
if (($nx > $ox) || ($ny > $oy))
{
$this->_debug($function, 'The image could not be resized because the size given is larger than the original image.');
return FALSE;
}
if($ox>$oy)
{
$nx = $ox*$ny/$oy;
}
elseif($ox<$oy)
{
$ny = $oy*$nx/$ox;
}
if ($this->_gdVersion == 2)
{
$this->_imgFinal = @ImageCreateTrueColor($nx, $ny);
@ImageCopyResampled($this->_imgFinal, $this->_imgOrig, 0, 0, 0, 0, $nx, $ny, $ox, $oy);
}
else
{
$this->_imgFinal = @ImageCreate($nx, $ny);
@ImageCopyResized($this->_imgFinal, $this->_imgOrig, 0, 0, 0, 0, $nx, $ny, $ox, $oy);
}
if($crop AND $nx!=$nnx AND $ny!=$nny)
{
$_imgOrigCopy = $this->_imgOrig;
$this->_imgOrig = $this->_imgFinal;
$result = $this->cropToSize($nnx, $nny);
$this->_imgOrig = $_imgOrigCopy;
unset($_imgOrigCopy);
return $result;
}
return true;
}
}
?> |
Last edited by amnuts on Tue Oct 10, 2006 4:58 am; edited 2 times in total |
|
Back to top |
|
 |
max0matic Newbie
Joined: 26 Feb 2004 Posts: 5
|
Posted: Fri Feb 27, 2004 11:50 am Post subject: Yes |
|
|
thats just what i need..
you should make this part of crop.canvas.. or
make a more general class that can do it all...
manipulation of images with a abstraction layer...
 |
|
Back to top |
|
 |
diversive Newbie
Joined: 04 Mar 2004 Posts: 2
|
Posted: Thu Mar 04, 2004 6:50 am Post subject: Crop, then resize... |
|
|
Mr Amnuts,
Your class is absolutly brilliant..
However, I must be really stupid. I'm sure I'm missing something obvious.
When I try to resize an image after cropping it. I have:
PHP: | <?php
$rs = new imgRedim();
$rs->loadImage($_POST['imageFile']);
$rs->cropToDimensions($_POST['sx'], $_POST['sy'], $_POST['ex'], $_POST['ey']);
$rs->redimToSize(45, 65, '?*?');
$rs->saveImage('final.jpg', 90);
$rs->flushImages();
?> |
?*? -> Don't know what to put here?!
However, it doesn't seem to work? All I get is the resized image without taking account of the cropping.
How do I get both in one pass? Or is this not possible?
Thanks.
Mark. |
|
Back to top |
|
 |
amnuts Site Admin

Joined: 01 Sep 2002 Posts: 662 Location: East Sussex, England
|
Posted: Thu Mar 04, 2004 7:17 am Post subject: Re: Crop, then resize... |
|
|
Hi Mark, and welcome to the board!
Thanks for your kind words about my class.
I haven't actually used the above user extension to my class, but if should take care of the cropping and resizing for you, so you shouldn't have to use both cropToDimension and redimToSize. You should be able to do something like this:
PHP: | <?php
$rs = new imgRedim();
$rs->loadImage($_POST['imageFile']);
$rs->redimToSize(45, 65, true);
$rs->saveImage('final.jpg', 90);
?> |
I have 'true' where you had '?*?' because it's a boolean parameter to perform a crop, or just to resize to that dimension (true or false, respectively). Try that out and see if it gives you the result you want.
What I personally have done in the past is use the crop-canvas class with my thumbnailer class (class.dropshadow.php). What I would do is resize using the greater control of the thumbnailer class, save the file and then crop it.
Regards,
Andy |
|
Back to top |
|
 |
checksum Newbie
Joined: 23 Aug 2004 Posts: 2
|
Posted: Mon Aug 23, 2004 5:36 pm Post subject: ARGGGHHHHHH!!!! |
|
|
It doesnt work!!!
All I want to do is take some large images form my gallery and thumb them to 75 * 75 thumbs?
Is this class supposed to do this , all it seems to achieve is a "closest proportional output";
ie
isnt $rs->redimToSize( 75, 75, TRUE );
supposed to take anything substantially larger shrink it as much as possibel then crop to the above dimensions.
If someone has a working example of this simple concept please post!!!
ARGGGGGHHHH!!!! |
|
Back to top |
|
 |
checksum Newbie
Joined: 23 Aug 2004 Posts: 2
|
Posted: Mon Aug 23, 2004 6:02 pm Post subject: less haste... |
|
|
Him me thinks my last post was a little hasty
omitting the
$nx!=$nnx AND $ny!=$nny) part
from the
if($crop AND $nx!=$nnx AND $ny!=$nny)
and it serves me well!!
very slick oop guys! |
|
Back to top |
|
 |
Jermaine Newbie
Joined: 06 Sep 2004 Posts: 4
|
Posted: Mon Sep 06, 2004 4:50 pm Post subject: ??? |
|
|
I read all posts to that new class, but don't get it working. I put in the code and i get an empty page. So all pathes are correct and no errors occur. But the page is just empty. can you be so kind and do a little tutorial how to implemet that class. Is it a new file? Do i have to put it in your class?
I am a little lost here.
I checked out your thumbnail shadow class and I think if the resizing could be done interactive too that would solve the problem.[/php] |
|
Back to top |
|
 |
amnuts Site Admin

Joined: 01 Sep 2002 Posts: 662 Location: East Sussex, England
|
Posted: Tue Sep 07, 2004 3:45 am Post subject: |
|
|
Copy the redim code above and place it into a new file. Call is something like class.redim.php, and save it in the same location as the class.cropcanvas.php file. The redim class (supplied by a member of the board) extends them crop canvas class, and so needs to have that class declared first.
So, you have the two files, then you might do something like:
PHP: | <?php
require_once('class.cropcanvas.php');
require_once('class.redim.php');
$rd = new imgRedim();
$rd->loadImage($_POST['imageFile']);
$rd->redimToSize(50, 50, true);
$rd->saveImage('final.jpg', 90);
?> |
That's probably the most simple use. It requires the main cropping class, then the redim class, which extends the cropping class. It then creates an instance of the redim class, loads an image, resizes it (and automatically crops if needs be) and then saves to a file called 'final.jpg'.
Because the redim class extends the main cropping class, you have all the functionality of the cropping class available to you in your instance, in this case $rd.
Does that make sense?
For more information on extending classes in PHP, check out the manual at: http://www.php.net/manual/en/keyword.extends.php
Andy |
|
Back to top |
|
 |
nation-x Newbie
Joined: 28 Aug 2005 Posts: 6
|
Posted: Sun Aug 28, 2005 12:42 pm Post subject: Class contains an error |
|
|
The redim class contains an error that prevents it from cropping images to specified size.
Original Code:
PHP: | <?php if($crop AND $nx!=$nnx AND $ny!=$nny) {
$_imgOrigCopy = $this->_imgOrig;
$this->_imgOrig = $this->_imgFinal;
$result = $this->cropToSize($nnx, $nny);
$this->_imgOrig = $_imgOrigCopy;
unset($_imgOrigCopy);
return $result;
} ?> |
The issue is that since we are resizing an image to aspect ratio based on a target dimension set then either $nx will always equal $nnx or $ny will always equal $nny. The fix is to place a boolean check to see whether cropping is needed before this statement as follows.
PHP: | <?php $cropNecessary = false;
if (($nx != $nnx) || ($ny != $nny)) {
$cropNecessary = true;
}
if ($crop && $cropNecessary) {
$_imgOrigCopy = $this->_imgOrig;
$this->_imgOrig = $this->_imgFinal;
$result = $this->cropToSize($nnx, $nny);
$this->_imgOrig = $_imgOrigCopy;
unset($_imgOrigCopy);
return $result;
} ?> |
|
|
Back to top |
|
 |
Leech Newbie
Joined: 20 Apr 2006 Posts: 1
|
Posted: Thu Apr 20, 2006 5:13 pm Post subject: |
|
|
Hi,
I really love your classes guys!
But I've a little problem..
I want to cropToSize a pic, with the width of 640 and height of 400.. it's ok.. I can do it..
but if I upload a pic that is 500 x 480.. the class doesn't resize the height..
I need to get the image at size of 500x400, bc the params I pass are maximums..
Any idea?
Thanks |
|
Back to top |
|
 |
nation-x Newbie
Joined: 28 Aug 2005 Posts: 6
|
Posted: Sun Apr 23, 2006 10:21 am Post subject: |
|
|
See the previous post  |
|
Back to top |
|
 |
KtM Newbie
Joined: 09 Jan 2007 Posts: 4
|
Posted: Tue Jan 09, 2007 12:14 pm Post subject: |
|
|
Is it possible to combine the imgRedim class with the cropinterface ?
I tried the following, but it doesn't work
PHP: | <?php require('class.cropinterface.php');
require_once('class.redim.php');
$ci =& new CropInterface(true);
if (isset($_GET['file'])) {
$ci->loadImage($_GET['file']);
$ci->cropToDimensions($_GET['sx'], $_GET['sy'], $_GET['ex'], $_GET['ey']);
$ci->redimToSize(150, 225, true);
$ci->saveImage('final.jpg', 90);
exit;
} ?> |
Thx in advance  |
|
Back to top |
|
 |
amnuts Site Admin

Joined: 01 Sep 2002 Posts: 662 Location: East Sussex, England
|
Posted: Tue Jan 09, 2007 3:55 pm Post subject: |
|
|
Hi, and welcome to the board!
If you wanted to do that you'd have to change the line:
Code: | class imgRedim extends canvasCrop |
to:
Code: | class imgRedim extends CropInterface |
and then change:
Code: | function imgRedim($debug = FALSE)
{
$this->canvasCrop($debug);
} |
to:
Code: | function imgRedim($debug = false)
{
parent::CropInterface($debug);
} |
Then you code would look like:
PHP: | <?php require('class.redim.php');
$ci =& new imgRedmin(true);
if (isset($_GET['file'])) {
$ci->loadImage($_GET['file']);
$ci->cropToDimensions($_GET['sx'], $_GET['sy'], $_GET['ex'], $_GET['ey']);
$ci->redimToSize(150, 225, true);
$ci->saveImage('final.jpg', 90);
exit;
} ?> |
Alternatively, and much, much more easily; just add the 'redimToSize' and '_reSize' methods to the CropInterface class itself and not both with calling the imgRedim at all.
Andy |
|
Back to top |
|
 |
KtM Newbie
Joined: 09 Jan 2007 Posts: 4
|
Posted: Wed Jan 10, 2007 9:53 am Post subject: |
|
|
thx
yesterday I tried it with
PHP: | <?php require('class.cropinterface.php');
require('class.redim.php');
$ci =& new CropInterface(true);
if (isset($_GET['file'])) {
$ci->loadImage($_GET['file']);
$ci->cropToDimensions($_GET['sx'], $_GET['sy'], $_GET['ex'], $_GET['ey']);
$ci->saveImage('final.jpg', 90);
$rd = new imgRedim();
$rd->loadImage('final.jpg');
$rd->redimToSize(150, 225, true);
$rd->saveImage('finalx.jpg', 90);
//$ci->showImage('png', 100);
exit;
} ?> |
It works, too ... but I think your solution is the better one
edit:
your 1st suggestion didn't work
got only a blank window.
but the 2nd (adding functions to cropinterface class) works fine  |
|
Back to top |
|
 |
KtM Newbie
Joined: 09 Jan 2007 Posts: 4
|
Posted: Thu Jan 11, 2007 10:29 am Post subject: |
|
|
KtM wrote: | but the 2nd (adding functions to cropinterface class) works fine  |
hhmm .. I have to correct myself ^^
it doesn't work like it should do
if I add the 2 functions to the cropinterface class and put the following code into cropinterface.php
PHP: | <?
require('class.cropinterface.php');
$ci =& new CropInterface(true);
if (isset($_GET['file'])) {
$ci->loadImage($_GET['file']);
$ci->cropToDimensions($_GET['sx'], $_GET['sy'], $_GET['ex'], $_GET['ey']);
$ci->redimToSize(150, 225, true);
$ci->showImage('jpg', 100);
exit;
}
?> |
I get:
- the picture cropped with dimensions set in "setCropPositionDefault()", but not the selected area.
- the cropped picture correctly resized
if I add the 2 functions to the cropinterface class and put the following code into cropinterface.php
PHP: | <?
require('class.cropinterface.php');
$ci =& new CropInterface(true);
if (isset($_GET['file'])) {
$ci->loadImage($_GET['file']);
$ci->cropToDimensions($_GET['sx'], $_GET['sy'], $_GET['ex'], $_GET['ey']);
$ci->redimToSize(150, 225, false);
$ci->showImage('jpg', 100);
exit;
}
?> |
(pls note the "$ci->redimToSize(150, 225, false);")
I get a picture resized to 300x225, nothing cropped.
if I add the 2 functions to the cropinterface class and put the following code into cropinterface.php
PHP: | <?
require('class.cropinterface.php');
$ci =& new CropInterface(true);
if (isset($_GET['file'])) {
$ci->loadImage($_GET['file']);
$ci->cropToDimensions($_GET['sx'], $_GET['sy'], $_GET['ex'], $_GET['ey']);
$ci->redimToSize(150, 225, true);
$ci->showImage('jpg', 100);
exit;
}
?> |
I get:
- the picture cropped with dimensions set in "setCropPositionDefault()", but not the selected area.
- the cropped picture correctly resized
if I add the 2 functions to the cropinterface class and put the following code into cropinterface.php
PHP: | <?
require('class.cropinterface.php');
$ci =& new CropInterface(true);
if (isset($_GET['file'])) {
$ci->loadImage($_GET['file']);
$ci->cropToDimensions($_GET['sx'], $_GET['sy'], $_GET['ex'], $_GET['ey']);
$ci->saveImage('final.jpg', 90);
$rd = new imgRedim();
$rd->loadImage('final.jpg');
$rd->redimToSize(150, 225, true);
$rd->showImage('jpg', 100);
exit;
}
?> |
I get a correctly cropped and resized picture, but the picture isn't shown at the end.
the only way to get what I want is to NOT add the 2 functions to the cropinterface class and to put the following code into cropinterface.php
PHP: | <?
require('class.cropinterface.php');
require('class.redim.php');
$ci =& new CropInterface(true);
if (isset($_GET['file'])) {
$ci->loadImage($_GET['file']);
$ci->cropToDimensions($_GET['sx'], $_GET['sy'], $_GET['ex'], $_GET['ey']);
$ci->saveImage('final.jpg', 90);
$rd = new imgRedim();
$rd->loadImage('final.jpg');
$rd->redimToSize(150, 225, true);
$rd->showImage('jpg', 100);
exit;
}
?> |
I get a correctly cropped and resized picture, and the picture is shown at the end.
A bit strange, isn't it ?
PS:
sorry for my "english"  |
|
Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|