sfhazel has asked for the wisdom of the Perl Monks concerning the following question:

Below is a bit of code from my program. I am having a difficult time getting the gdImageCreateFromGd2Part function to work. The online documentation doesn't seem to be enough for me. I need to see an example of the function in use so I can see why my code is wrong. Please help my monk friendlings! *bows*
my $srcimage = GD::Image->newFromJpeg($target_dir."/".$newName); my $newimage = new GD::Image(100,100); $newimage->gdImageCreateFromGd2Part($srcimage,100,100,100,100);

Replies are listed 'Best First'.
Re: gdImageCreateFromGd2Part
by Tanktalus (Canon) on Sep 28, 2005 at 23:32 UTC

    Out of curiosity - being somewhat of a GD newbie myself, can you provide enough of a script to show what you're getting vs what you're expecting? For example, are you saving $newimage to a file and expecting something there, or are you displaying it to a Tk window, or ...?

    Thanks!

      My goal is to select an area from the existing photo and then save it. Right now I'm stuck on the gdImageCreateFromGd2Part function. Here is the actual chunk of code that I'm running. I'm going to have two parts, which execute depending on if the image has more or less than 4 million pixels. If it has less than that number, it uses copyResized. This part works. I'm working on the part that processes images with more than that many pixels. The code works fine apart from the line with the gdImageCreateFromGd2Part function in it. Hope this more detailed code helps:
      my $srcimage = GD::Image->newFromJpeg($target_dir."/".$newName); my ($srcW,$srcH) = $srcimage->getBounds(); if( $srcW*$srcH > 4000000 ){ my $newimage = new GD::Image(100,100); $newimage->gdImageCreateFromGd2Part($srcimage,100,100,100,100); # future code to make a thumbnail from the part that is pulled fro +m the image }else{ my $maxheight = 400; my $maxwidth = 400; my $wdiff = $srcW - $maxwidth; my $hdiff = $srcH - $maxheight; my $newH; my $newW; if ($wdiff > $hdiff){ $newW = $maxwidth; $aspect = ($newW/$srcW); $newH = int($srcH * $aspect); }else{ $newH = $maxheight; $aspect = ($newH/$srcH); $newW = int($srcW * $aspect); } my $newimage = new GD::Image($newW,$newH); $newimage->copyResized($srcimage,0,0,0,0,$newW,$newH,$srcW,$srcH); open(FILE, ">".$target_dir."/preview/".$newName) || die "Cannot op +en image: $!\n"; print FILE $newimage->jpeg; }
Re: gdImageCreateFromGd2Part
by BrowserUk (Patriarch) on Sep 29, 2005 at 01:19 UTC

    Are you by any chance converting a script from another language or perhaps using the gdlib documentation rather than the GD docs?

    I ask, because I cannot see any evidence of a function called gdImageCreateFromGd2Part() being exported from GD.pm.

    I think that the function you are looking for is exported from GD as $image = GD::Image->newFromGd2Part($file,srcX,srcY,width,height).


    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
    "Science is about questioning the status quo. Questioning authority".
    The "good enough" maybe good enough for the now, and perfection maybe unobtainable, but that should not preclude us from striving for perfection, when time, circumstance or desire allow.
Re: gdImageCreateFromGd2Part
by zentara (Cardinal) on Sep 29, 2005 at 13:01 UTC
    Here is a GD based "slice-and-dice" script I use to make composite clickable image files.
    #!/usr/bin/perl use warnings; use strict; use GD; my @tiles = (); my $file = shift || '2uni2.jpg'; my $tempname = $file; $tempname =~ s/^(.+)(\.\w+)$/$1/; print "$tempname\n"; #set tile size my $x = 100; my $y = 100; my $image = GD::Image->newFromJpeg($file,0); my ($width,$height) = $image->getBounds(); print "width->$width height->$height\n"; my $rows = int($height/$y +1) - 1; #make it 0 based my $cols = int($width/$x + 1) - 1; print "rows->$rows cols->$cols\n"; foreach my $row(0..$rows){ foreach my $col(0..$cols){ my $imageout = new GD::Image($x,$y); #$image->copy($sourceImage,$dstX,$dstY, # $srcX,$srcY,$width,$height) $imageout->copy($image,0,0,($col*$y),($row*$x),$x,$y); my $tilename = $tempname .'-'.$row.'-'.$col.'.jpg'; push(@tiles,$tilename); open(OUT, '>', $tilename) or warn $!; print OUT $imageout->jpeg; close OUT; } }

    I'm not really a human, but I play one on earth. flash japh

      (can you tell Perl isn't my normal server side langauge?)

      BrowserUK. Yes, I was converting that particular function from another language. However, the function replacement wasn't working for me. Pointing out my error was helpful in locating documentation that was Perl specific.

      Zentara. Thank you for this bit of documented code. I am now able to get the cropped images I was looking for.

      $image->copy($sourceImage,$dstX,$dstY,$srcX,$srcY,$width,$height);

      Thank you guys both for the quick and straight-foward help.

      I was getting a cropped image because the image files I'm uploading are really big, and using copyresized causes the server to timeout and send back an internal server message. Well, even though the script seems to be working on files slightly larger than ones that failed with the old script, the server still times out for the REALLY big ones. Is there anyway to resize an image without putting so much strain on the server? For instance, crop the image without actually having to open it?