Problem with rezising images ...

See all posts Reply

Problem with rezising images ... new!
by Stefan, 15 years, 5 months ago
Colin, I have successfully got it to upload files to the server, but when I'm trying to crop and rezise I got into some trouble ... Maybe u can help me out?

I have one file on the server: pic400px_original.jpg
It is width: 400px and height: 495px

Now I need to crop and rezise it.
Crop is working, but file is saved in the crop size, say 363px by 363px
and not 400px by 400px as I need it rezised to ...
( Later I also need 128px by 128px and 48px by 48px ... When this works ;) )

So, can you check my code below and see if you can tell me where I messed up?

$ul_handle = new upload($fname_original);  // members/profileimg/0000001_0000500/0000001/pic400px_original.jpg
if ($ul_handle->uploaded) {
 $px_filename = "pic400px";
 $ul_handle->image_x = 400;
 $ul_handle->image_y = 400;
 $ul_handle->file_overwrite = true;
 $ul_handle->file_auto_rename = false;
 $ul_handle->image_ratio_crop = true;
 $ul_handle->file_new_name_body = $px_filename;
 $ul_handle->image_convert = "jpg";  // Make sure I always get jpg files: png, jpg, gif, bmp
 $ul_handle->image_crop = array(34, 0, 98, 37);
 $ul_handle->Process($upload_path);  // members/profileimg/0000001_0000500/0000001
}
Reply
Hmmm.... new!
by DaveW, 15 years, 5 months ago
Stefan,

I'd have thought you should crop first, THEN resize the image!! I'm trying to do the same thing at the moment, but it's not working properly for me at all, I only came on to see if there was an answer to the issue!!!

Cheers

DaveReply
Re: Hmmm.... new!
by DaveW, 15 years, 5 months ago
That said....

I've just downloaded the new version, anc exactly the same problem.

This is my code....
$t = CCGetParam("top") ;
$l = CCGetParam("left") ;
$h = CCGetParam("height") ;
$w = CCGetParam("width") ;
$rh = CCGetParam("reqh") ;
$rw = CCGetParam("reqw") ;

$b = $handle->image_src_y - ($t + $h) ;
$r = $handle->image_src_x - ($l + $w) ;

$handle->image_crop 	= array($t, $r, $b, $l);
$handle->image_resize          = true;
$handle->image_y	       = $rh;
$handle->image_x               = $rw;
$handle->image_convert         = 'jpg';
$handle->jpeg_quality          = 80;

$handle->Process('../tmp/') ;

If I comment out the resize, the crop works fine (and as expected)

If I comment out the crop, the resize works as expected!

Together, I get a jpeg image of 1x1

???????

Please helpReply
Re: Problem with rezising images ... new!
by Stefan, 15 years, 5 months ago
I was trying to do it all in one step, but can't get it to work ...
It just don't do the rezise thing, just the cropping ...

So, I will do some test to crop first, then do the rezise ...

I was hoping to get some input from Colin what would be the correct way to do it ...

Anyway, I'll do some more testing now ... I'll drop a line here leter what result I get...
Also, if you find out a way to do it, let me know ;)Reply
Re: Problem with rezising images ... new!
by colin, 15 years, 5 months ago
I am investigating.......Reply
Re: Problem with rezising images ... new!
by Stefan, 15 years, 5 months ago
Colin and Dave ...
I got it working now ...
You can check my code pls give comments if I do something stupid
or it can be done in a better way ...

Anyway, I have uploaded an original image of 400x495 and this code
crop out 4 new images: 49x48, 128x128, 256x256 and 400x400

I use a form where I drag a square over the original to select the are I want in the cropped images, and now it works fine, just as I want it ...

I just want to know if I did it in a correct way or if it can be done better???

BTW Colin: I sent u a donation a few hrs ago for a license :)

$max_width  = "400";
$max_height = "400";
$upload_path = get_profile_image_path($id, $typ, "");
$fname_original = $upload_path."/".$fname;
list($original_width, $original_height, $type, $attr) = getimagesize($fname_original);
$arr_size[] = "400";
$arr_size[] = "256";
$arr_size[] = "128";
$arr_size[] = "048";
$n = count($arr_size);

for ($i=0; $i<$n; $i++) {

 // T R B L
 $top     = $input_top;
 $right   = $original_width - $input_left - $input_width;
 $bottom  = $original_height - $input_height - $input_top;
 $left    = $input_left;
 $file_new_name_body = "pic".$arr_size[$i]."px";

 // Start Step 1 - Crop
 $ul_handle = new upload($fname_original);  
 if ($ul_handle->uploaded) {
  $ul_handle->image_x             = $arr_size[$i];
  $ul_handle->image_y             = $arr_size[$i];
  $ul_handle->jpeg_quality        = 80;
  $ul_handle->file_overwrite      = true;
  $ul_handle->file_auto_rename    = false;
  $ul_handle->image_ratio_crop    = true;
  $ul_handle->file_new_name_body  = $file_new_name_body;
  $ul_handle->image_crop          = array($top, $right, $bottom, $left);  // T R B L
  $ul_handle->Process($upload_path);  
 }
 // End Step 1 - Crop

 // Start Step 2 - Resize
 $ul_handle = new upload($upload_path."/".$file_new_name_body.".jpg"); 
 if ($ul_handle->uploaded) {
  $ul_handle->jpeg_quality        = 80;
  $ul_handle->image_resize        = true;
  $ul_handle->file_overwrite      = true;
  $ul_handle->file_auto_rename    = false;
  $ul_handle->image_ratio_y       = true;
  $ul_handle->image_x             = $arr_size[$i];
  $ul_handle->file_new_name_body  = $file_new_name_body;
  $ul_handle->Process($upload_path); 
 }
 // End Step 2 - Resize
}
Reply
Re: Problem with rezising images ... new!
by colin, 15 years, 5 months ago
What you do is valid, but you are losing quality by processing each image twice.

The class should be able to resize and crop at the same time (the cropping would happen after the resizing, as it is the way the class works). But there seem to be an issue here...

I will have an in depth look at it tonight.

You sent me an email for a license? I haven't received anything; would you please be kind enough as to resend it?Reply
Re: Problem with rezising images ... new!
by Stefan, 15 years, 5 months ago
Yes, that was the problem I had ... My 2 step "temporary" solution works,
but I really looking forward to see your 1 step solution ... Hopefully 2morrow :)))

On http://www.verot.net/php_class_upload_license.htm I clicked the PayPal link
and sent you 15 EUR for a commercial license ...

Donation Details

Confirmation number: 1PY957874V587602Y
Donation amount: €15.00 EUR
Total: €15.00 EUR
Purpose: class.upload commercial license
Contributor: Stefan Fahlberg

Check your mail, it's probably there !

/StefanReply
Re: Problem with rezising images ... new!
by colin, 15 years, 5 months ago
Thank you for the donation. I however haven't received any email, and therefore don't have your email address. Could you please email me, so I can send you back the license document?Reply
Re: Problem with rezising images ... new!
by DaveW, 15 years, 5 months ago
Stephan,

I haven't tried this, but expected that it'd work because it's a two step process (which I wanted to avoid). I'll wait for Colin to finish his investigations before I commit to a 2 step process.

Cheers

Dave

N.B. A a particular thanks to Colin for his quick response.Reply
Re: Problem with rezising images ... new!
by Stefan, 15 years, 5 months ago
I'll use my 'temporary' working code until Colin have a better solution working.
Anyway, my site is not live yet, so it's just me sending up test pictures :-)

/StefanReply
Re: Problem with rezising images ... new!
by colin, 15 years, 5 months ago
The class as version 0.26 cannot do what you want.

So here is a Release Candidate 0.27RC1, which implements a mean to crop before resizing.

The cropping is set with image_precrop, which accepts the same type of parameters as image_crop.

For instance:
$handle->image_precrop = array(-34, 0, 98, 100);
This will crop the image before eventually resizing it.

Note that both image_precropand image_crop can be used together. The first one crops before resizing, the second one after resizing.

The test suite looks OK, but I haven't tested it extensively. Feedback would be appreciated.Reply
Re: Problem with rezising images ... new!
by Stefan, 15 years, 5 months ago
Colin, with 0.27RC1 it works like a charm :)

See my "1 step" code below (and pls give comments if u think it can be done better) :

BTW ... Did u get any confirmation from PayPal about my transaction ?

include("./core/class.upload.php"); // 0.27RC1

$max_width  = "400";
$max_height = "400";

$upload_path = get_profile_image_path($id, $typ, "");

$fname_original = $upload_path."/".$fname;

list($original_width, $original_height, $type, $attr) = getimagesize($fname_original);

$arr_size[] = "400";
$arr_size[] = "256";
$arr_size[] = "128";
$arr_size[] = "048";
$n = count($arr_size);

for ($i=0; $i<$n; $i++) {

 // T R B L
 $top     = $input_top;
 $right   = $original_width - $input_left - $input_width;
 $bottom  = $original_height - $input_height - $input_top;
 $left    = $input_left;

 $file_new_name_body             = "pic".$arr_size[$i]."px";

 // Start - Crop
 $ul_handle = new upload($fname_original);

 if ($ul_handle->uploaded) {
  $ul_handle->image_x             = $arr_size[$i];
  $ul_handle->image_y             = $arr_size[$i];
  $ul_handle->image_resize        = true;
  $ul_handle->jpeg_quality        = 80;
  $ul_handle->file_overwrite      = true;
  $ul_handle->file_auto_rename    = false;
  $ul_handle->image_ratio_crop    = true;
  $ul_handle->file_new_name_body  = $file_new_name_body;
  $ul_handle->image_convert       = "jpg";  // png, jpg, gif, bmp
  $ul_handle->image_precrop       = array($top, $right, $bottom, $left);
  $ul_handle->Process($upload_path);
 }
 // End - Crop
}
Reply
Re: Problem with rezising images ... new!
by colin, 15 years, 5 months ago
You code looks all good to me.

I haven't received any email from Paypal, it may have gone lost. But the donation itself has arrived. Thanks again.
I therefore don't have your email address. Could you please email me, so I can send you back the license document?Reply
Re: Problem with rezising images ... new!
by Stefan, 15 years, 5 months ago
Great, and thank's for having a look :-)
I tested some more and it works super fine !!!

I just sent you a mail for the license thing ... Have a look.

Take Care!

/StefanReply