andy's php projects
 FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister   ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 
Script and site usage:
All of the scripts on this site are provided free for non-commerical, personal or non-profit use. If you find these scripts useful to you, appreciate the free support, or are just an all round nice person, then why not donate a little dosh? If you wish to use any of these in a commercial system, please refer to the payment plan.


User submitted class; 'imgRedim'
Goto page 1, 2  Next
 
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    php.amnuts.com Forum Index -> Crop Canvas
View previous topic :: View next topic  
Author Message
amnuts
Site Admin


Joined: 01 Sep 2002
Posts: 662
Location: East Sussex, England

PostPosted: Mon Dec 01, 2003 5:13 am    Post subject: User submitted class; 'imgRedim' Reply with quote

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->_imgOrig0000$nx$ny$ox$oy);
        }
        else
        {
            
$this->_imgFinal = @ImageCreate($nx$ny);
            @
ImageCopyResized($this->_imgFinal$this->_imgOrig0000$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
View user's profile Send private message Send e-mail Visit poster's website AIM Address Yahoo Messenger MSN Messenger
max0matic
Newbie


Joined: 26 Feb 2004
Posts: 5

PostPosted: Fri Feb 27, 2004 11:50 am    Post subject: Yes Reply with quote

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...

Wink
Back to top
View user's profile Send private message
diversive
Newbie


Joined: 04 Mar 2004
Posts: 2

PostPosted: Thu Mar 04, 2004 6:50 am    Post subject: Crop, then resize... Reply with quote

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(4565'?*?');
$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
View user's profile Send private message
amnuts
Site Admin


Joined: 01 Sep 2002
Posts: 662
Location: East Sussex, England

PostPosted: Thu Mar 04, 2004 7:17 am    Post subject: Re: Crop, then resize... Reply with quote

Hi Mark, and welcome to the board!

Thanks for your kind words about my class. Smile

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(4565true);
$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
View user's profile Send private message Send e-mail Visit poster's website AIM Address Yahoo Messenger MSN Messenger
checksum
Newbie


Joined: 23 Aug 2004
Posts: 2

PostPosted: Mon Aug 23, 2004 5:36 pm    Post subject: ARGGGHHHHHH!!!! Reply with quote

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
View user's profile Send private message
checksum
Newbie


Joined: 23 Aug 2004
Posts: 2

PostPosted: Mon Aug 23, 2004 6:02 pm    Post subject: less haste... Reply with quote

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
View user's profile Send private message
Jermaine
Newbie


Joined: 06 Sep 2004
Posts: 4

PostPosted: Mon Sep 06, 2004 4:50 pm    Post subject: ??? Reply with quote

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
View user's profile Send private message
amnuts
Site Admin


Joined: 01 Sep 2002
Posts: 662
Location: East Sussex, England

PostPosted: Tue Sep 07, 2004 3:45 am    Post subject: Reply with quote

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(5050true); 
$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
View user's profile Send private message Send e-mail Visit poster's website AIM Address Yahoo Messenger MSN Messenger
nation-x
Newbie


Joined: 28 Aug 2005
Posts: 6

PostPosted: Sun Aug 28, 2005 12:42 pm    Post subject: Class contains an error Reply with quote

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
View user's profile Send private message
Leech
Newbie


Joined: 20 Apr 2006
Posts: 1

PostPosted: Thu Apr 20, 2006 5:13 pm    Post subject: Reply with quote

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
View user's profile Send private message
nation-x
Newbie


Joined: 28 Aug 2005
Posts: 6

PostPosted: Sun Apr 23, 2006 10:21 am    Post subject: Reply with quote

See the previous post Smile
Back to top
View user's profile Send private message
KtM
Newbie


Joined: 09 Jan 2007
Posts: 4

PostPosted: Tue Jan 09, 2007 12:14 pm    Post subject: Reply with quote

Is it possible to combine the imgRedim class with the cropinterface ?
I tried the following, but it doesn't work Sad
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(150225true); 
    
$ci->saveImage('final.jpg'90);
    exit;
?>


Thx in advance Wink
Back to top
View user's profile Send private message
amnuts
Site Admin


Joined: 01 Sep 2002
Posts: 662
Location: East Sussex, England

PostPosted: Tue Jan 09, 2007 3:55 pm    Post subject: Reply with quote

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(150225true); 
    
$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
View user's profile Send private message Send e-mail Visit poster's website AIM Address Yahoo Messenger MSN Messenger
KtM
Newbie


Joined: 09 Jan 2007
Posts: 4

PostPosted: Wed Jan 10, 2007 9:53 am    Post subject: Reply with quote

thx Wink
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(150225true); 
    
$rd->saveImage('finalx.jpg'90);
    
//$ci->showImage('png', 100);
    
exit;
?>


It works, too ... but I think your solution is the better one Wink

edit:
your 1st suggestion didn't work Sad
got only a blank window.

but the 2nd (adding functions to cropinterface class) works fine Wink
Back to top
View user's profile Send private message
KtM
Newbie


Joined: 09 Jan 2007
Posts: 4

PostPosted: Thu Jan 11, 2007 10:29 am    Post subject: Reply with quote

KtM wrote:
but the 2nd (adding functions to cropinterface class) works fine Wink

hhmm .. I have to correct myself ^^
it doesn't work like it should do Sad

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(150225true);
    
$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(150225false);
    
$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(150225true);
    
$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(150225true);
    
$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(150225true);
    
$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" Wink
Back to top
View user's profile Send private message
Display posts from previous:   
This forum is locked: you cannot post, reply to, or edit topics.   This topic is locked: you cannot edit posts or make replies.    php.amnuts.com Forum Index -> Crop Canvas All times are GMT - 5 Hours
Goto page 1, 2  Next
Page 1 of 2

 
Jump to:  
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