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.


Scaling image to fit canvas, then crop it?

 
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
MarkPW
Newbie


Joined: 24 Aug 2005
Posts: 3

PostPosted: Wed Aug 24, 2005 6:21 pm    Post subject: Scaling image to fit canvas, then crop it? Reply with quote

Hi there,

I've come across your class today after spending two days figuring out why imagemagick wouldn't execute via php's shell function.

What I need to do is resize an image and crop that same image in the center to create thumbnails. So far I've managed to crop using cropToSize(), however, what this does is leaves the original size of the image as is, crops out of the dimensions I specify and saves it.

For example, I need to resize an 150x200 image to fit into a 110x150 thumbnail but keep it in proportion with the original dimensions. In this case the thumbnail would need to resize to a height of 150 which would make the width 113. I then need to crop this to the width of 110.

I'm sorry for not being able to explain this very well. To put it simply, I need the 150x200 image to fit within an area of 110x150 but so the image resizes to the width/height (which ever is larger) without losing proportion and then crop the remaining area.

Is this possible to do with the cropcanvas class?

Also, is it possible to use remote images? I tried using remote images when testing it out but it wouldn't work. GD does support remote images through the imagecreatefrom...() function, so I'm a little baffled why this doesn't seem possible with your script.

I would very much appreciate any help with this.

Thanks Confused
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 Aug 25, 2005 5:05 am    Post subject: Re: Scaling image to fit canvas, then crop it? Reply with quote

Hi, and welcome to the board!

MarkPW wrote:
I've come across your class today after spending two days figuring out why imagemagick wouldn't execute via php's shell function.


That can be tricky, yeah. If you have the option of installing PHP extensions, then there's one for ImageMagick that you might want to try out.

http://pecl.php.net/package/imagick

MarkPW wrote:
What I need to do is resize an image and crop that same image in the center to create thumbnails. So far I've managed to crop using cropToSize(), however, what this does is leaves the original size of the image as is, crops out of the dimensions I specify and saves it.

For example, I need to resize an 150x200 image to fit into a 110x150 thumbnail but keep it in proportion with the original dimensions. In this case the thumbnail would need to resize to a height of 150 which would make the width 113. I then need to crop this to the width of 110.


This has been covered on the message board before.

As standard, the crop canvas class just does cropping. I have another class that gives all manor of thumbnailing options. It's called 'drop shadow', but don't let the name fool you - it's a thumbnailing class more than a drop shadow class!

What you could do (which I do myself), if use both classes in tandem, resizing with one, passing over the resized image to the other for cropping and saving.

A simple example:

PHP:
<?php

require_once('class.dropshadow.php');
require_once(
'class.cropcanvas.php');

$ds =& new dropShadow();
$cc =& new canvasCrop();

define('IMAGE_DIR',  '/home/test/');
define('THUMB_SIZE'120);
define('CROP_W',     150);
define('CROP_H',     150);

$file 'image.jpg';

$img getimagesize(IMAGE_DIR $file);
if (
$ds->loadImage(IMAGE_DIR $file)) {
    if (
$img[0] > $img[1]) {
        
$ds->resizeToSize(0THUMB_SIZE);
    } else {
        
$ds->resizeToSize(THUMB_SIZE0);
    }
    
$cc->_imgOrig $ds->_imgFinal;
    
$cc->cropToSize(CROP_WCROP_HccCENTRE);
    
$cc->saveImage(IMAGE_DIR 'thumb_' $file);
}

?>


Of course, you could always create an extension to the crop canvas class that does the resizing for you first and then crops the image.

MarkPW wrote:
Also, is it possible to use remote images? I tried using remote images when testing it out but it wouldn't work. GD does support remote images through the imagecreatefrom...() function, so I'm a little baffled why this doesn't seem possible with your script.


To be honest, up until just the other day I didn't realise imagecreatefromx worked with the file wrappers... Embarassed Guess I should read hte manual a bit more often/throughly, eh? Wink

So there are three reasons that spring to mind. First, the checking that the file exists. I never invisioned this being used for remote images, and have this check in the loadImage method:

PHP:
<?php if (!@file_exists($filename))
        {
            
$this->_debug('loadImage'"The supplied file name '$filename' does not point to a readable file.");
            return 
false;
        } 
?>


The file_exists method doesn't work with remote files on PHP4 and only some URL forms in PHP5. So either remove that block altogether or update it to check if it's a local file before checking.

Secondly, your file wrappers are turned off.

Thirdly, you have PHP less than 4.3.0.

But I'm going to hazzard a bet that it's the first reason.

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
MarkPW
Newbie


Joined: 24 Aug 2005
Posts: 3

PostPosted: Thu Aug 25, 2005 6:47 am    Post subject: Reply with quote

Your a God-send Andy! This will be great for now (and I got URL's to work aswell), but to be honest with you I don't like using someone elses work in my own work because it's harder to understand everything going on within it, plus it's nice to create your own scripts especially for commercial projects. And so at the moment I'm not sure whether I will be using this commercially, although if I do I'll be sure to license it. With your script I now know 100% that GD can do the stuff I need, so I thank you for actually demonstrating with your script that it is possible to do this stuff. Smile

Apologies if I posted a similar question to what has been asked before - I was tired from searching so much yesterday. Imagemagick seems such a good program, it just doesn't seem very well documented with regard to using it within php. If only I could get it to work via php without getting permission problems, I'd only need to enter 2 lines of code to do the same thing. Also, thanks for the link to the php extension, I will check it out!

Thank you again!!

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 Aug 25, 2005 9:22 am    Post subject: Reply with quote

Hi MArk,

totally understand about wanting to roll your own solution. Not only are then then very familiar with it, but there's always the pride of doing it, too. Smile I mentioned to commerical side of things just because I've started to do that for everyone that seems like they might be using it in a commercial setting... It wasn't anything personal. Smile If you end up not using it, or using it just as something to learn from, then that's just great and I hope it helps out!

No worries about posting the same kind of question. I only really mentioned that in case you wanted to do a search on the board for any additional info other than what I posted above. Thinking about it, though, the above is probably a much nicer and condensed version than you'd find elsewhere on the board. Wink

As for imagemagick; how are you trying to execute it? It might be that you need to tweak your PHP settings to allow shell access, or perhaps IM isn't set up correctly? Have you tried testing it from the shell?

I set up a system a few years ago that used IM for the image processing, and I loved how it worked. What I did was first to set up an array of the commands I'd be using, such as:

PHP:
<?php // setup image magick options
        
if (strtoupper(substr(PHP_OS03)) == 'WIN')
        {
            
// using ImageMagick for Win2k
            
$this->magickStrings = array(
                    
// parameters : orig file name (inc. path), size, thumb/mid file name (inc. path)
                    
"make thumbnail" => "convert -colorspace RGB -units PixelsPerInchResolution -density 72 \"%s\" +profile \"*\" -resize \"%s>\" \"%s-small.jpg\"  >NUL",
                    
"make midsized"  => "convert -colorspace RGB -units PixelsPerInchResolution -density 72 \"%s\" +profile \"*\" -resize \"%s>\" \"%s-mid.jpg\"  >NUL",
                    
"convert type"   => "convert \"%s[0]\" \"%s.%s\"" // no background
                
);
        }
        else
        {
            
// using ImageMagick for FreeBSD/Linux
            
$this->magickStrings = array(
                    
// parameters : orig file name (inc. path), size, thumb/mid file name (inc. path)
                    
"make thumbnail" => "/usr/local/bin/convert -colorspace RGB -units PixelsPerInchResolution -density 72 \"%s[0]\" +profile \"*\" -resize \"%s>\" \"%s-small.jpg\" >/dev/null &",
                    
"make midsized"  => "/usr/local/bin/convert -colorspace RGB -units PixelsPerInchResolution -density 72 \"%s[0]\" +profile \"*\" -resize \"%s>\" \"%s-mid.jpg\" >/dev/null &",
                    
"convert type"   => "/usr/local/bin/convert \"%s[0]\" \"%s.%s\" >/dev/null" // no background
                
);
        } 
?>


and then executed it something like this:

PHP:
<?php $id  uniqid("img_");
$fullTemp $tmpPath $id '.' $_POST['format'];
$cmd sprintf($this->magickStrings['convert type'], $filePath.$fName$tmpPath.$id$_POST['format']);
$foo exec($cmd); ?>


Are you using exec, or backtick, or system, etc.?

Andy
Back to top
View user's profile Send private message Send e-mail Visit poster's website AIM Address Yahoo Messenger MSN Messenger
MarkPW
Newbie


Joined: 24 Aug 2005
Posts: 3

PostPosted: Thu Aug 25, 2005 10:27 am    Post subject: Reply with quote

I completely understand about the commercial thing, I didn't take that personally so no worries there!

With regard to Imagemagick, Now I own the server, nobody else uses it but me and I'm trying to execute the following code in a PHP file:

PHP:
<?php

shell_exec
("convert http://www.website.com/pic1.jpg -resize x150 \
-gravity center -crop 110x150+0+0 /home/mysite/public_html/content/thumb1.jpg"
);

?>


The command, when entered directly into SSH worked fine yesterday, but I couldn't get it to work using shell_exec(). I decided to re-install IM through the RPM provided through cPanel WHM but that turned out to be an old version. So I uninstalled it by entering 'rpm -e ImageMagick' into SSH and then installed the latest version of IM again. What I then found was that IM would not work with remote URL's and would throw up the error, "Error [//www.website.com/pic1.jpg] Could not find file/directory" (or something like that!), by this time I found out that adding '2>&1' to the end of the shell_exec() command would show any errors and it did. So then I tried converting a local image (instead of a remote on) and outputting that locally, this worked ok in SSH but in the php script, it gave the error, 'Cannot open file /home/mysite/public_html/content/thumb1.jpg permission denied' (this is the output image btw). That particular directory is set to 777 and the image obviously doesn't exist becuase it's supposed to be the converted file, so I'm now wondering whether it's an ownership problem.

As you can see I have 2 problems.
1. IM will no longer work with remote files [in both SSH (putty) and PHP] and...
2. IM gives me a permission denied error when trying to save the converted image. [this particular error is only outputted when using shell_exec() via PHP - I get no such problems directly through shell]

So I've now uninstalled IM, somewhat annoyed and confused. By this time I tracked back to your script which, thanks to you, works for me. But I would really like to use IM's shell functions via PHP.

Btw. I also own another server where I've installed IM without problem, and although I don't have remote URL problems with this one I do get the same permission errors when using shell_exec() and all the other shell functions [exec() etc].

If you could shed any light on these problems Andy that I'd be very greatful! I know this really is nothing to do with your script, but I'm out of ideas and it seems nobody visits IM forums. My threads on there have gone unanswered.

Cheers Smile
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 Aug 25, 2005 11:08 am    Post subject: Reply with quote

Hmm.. Not sure about all the problems you've had installing IM as I've not run into those. However, the permissions thing... What you have to remember is that typically PHP doesn't run under your uid but under Apache's (assuming you've got Apache as your web server). Make sure you haven't got safe_mode turned on. Also, check the permissions for that folder... Even though you have it 777 maybe there's an ACL on the folder?

Andy
Back to top
View user's profile Send private message Send e-mail Visit poster's website AIM Address Yahoo Messenger MSN Messenger
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
Page 1 of 1

 
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