The Web Design Group

... Making the Web accessible to all.

Welcome Guest ( Log In | Register )

 
Reply to this topicStart new topic
> PHP Script for Resizing/Cropping an Image
j4v1
post May 21 2008, 10:10 AM
Post #1





Group: Members
Posts: 5
Joined: 15-May 08
Member No.: 5,666



Hello everyone. I'm battling an issue w/ a gif, which I'd like to crop/resize in a certain area. But when I use the below code it does not allow me to crop a particular region/area of the gif.

Below is the sample code. Also, if you follow the link to the original gif and then in your favorite editor crop the image so that its at the following dimension 438x519 from the center. I took the following pixels (left 0, Right 119, Top 476, Bottom 204). This above result is what I'm trying to get the following code to produce.

Any assistance again you may provide would truly be appreciated.

Thank you,
j4v1

<head>
<title>Untitled 2</title>
</head>

<body>
<?php
cropImage(500, 460, 'http://images.wsdot.wa.gov/nwflow/flowmaps/sysvert.gif', 'gif', 'test500x460.gif');
function cropImage($nw, $nh, $source, $stype, $dest) {
$size = getimagesize($source);
$w = $size[0];
$h = $size[1];
switch($stype) {
case 'gif':
$simg = imagecreatefromgif($source);
break;
case 'jpg':
$simg = imagecreatefromjpeg($source);
break;
case 'png':
$simg = imagecreatefrompng($source);
break;
}
$dimg = imagecreatetruecolor($nw, $nh);
$wm = $w/$nw;
$hm = $h/$nh;
$h_height = $nh/2;
$w_height = $nw/2;
if($w > $h) {
$adjusted_width = $w / $hm;
$half_width = $adjusted_width / 2;
$int_width = $half_width - $w_height;
imagecopyresampled($dimg,$simg,-$int_width,0,0,0,$adjusted_width,$nh,$w,$h);
} elseif(($w < $h) || ($w == $h)) {
$adjusted_height = $h / $wm;
$half_height = $adjusted_height / 2;
$int_height = $half_height - $h_height;
imagecopyresampled($dimg,$simg,0,-$int_height,0,0,$nw,$adjusted_height,$w,$h);
} else {
imagecopyresampled($dimg,$simg,0,0,0,0,$nw,$nh,$w,$h);
}
imagejpeg($dimg,$dest,100);
}
?>
</body>

</html>
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
Brian Chandler
post May 21 2008, 01:07 PM
Post #2


Jocular coder
********

Group: Members
Posts: 2,298
Joined: 31-August 06
Member No.: 43



You need to use a *square-bracket*CODE*end-square-bracket* thingy, so we can see the indentation.

The first big problem with this program is that it has no comments. How am I supposed to know what $nw is? What _precisely_ is it?

Here's the manual:
imagejpeg() creates a JPEG file from the given image .

Your call to imagejpeg() seems to write the image as a jpeg[!] to the file "something.gif"
That's a pretty odd thing to do - at least call it something.jpg.

But anyway, nothing I can see writes anything to the web page, so you should see a blank page. Perhaps it would be more usual to include a copy of the image
<img src="something.jpg">

But look, more basically, it just isn't reasonable to expect other people to debug your programs just by gazing at them. You have to investigate, and ask specific questions - like perhaps "Why does this produce a blank page?" which I hope I've already answered.
User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post
j4v1
post May 21 2008, 02:51 PM
Post #3





Group: Members
Posts: 5
Joined: 15-May 08
Member No.: 5,666



QUOTE(Brian Chandler @ May 21 2008, 11:07 AM) *

You need to use a *square-bracket*CODE*end-square-bracket* thingy, so we can see the indentation.

The first big problem with this program is that it has no comments. How am I supposed to know what $nw is? What _precisely_ is it?

Here's the manual:
imagejpeg() creates a JPEG file from the given image .

Your call to imagejpeg() seems to write the image as a jpeg[!] to the file "something.gif"
That's a pretty odd thing to do - at least call it something.jpg.

But anyway, nothing I can see writes anything to the web page, so you should see a blank page. Perhaps it would be more usual to include a copy of the image
<img src="something.jpg">

But look, more basically, it just isn't reasonable to expect other people to debug your programs just by gazing at them. You have to investigate, and ask specific questions - like perhaps "Why does this produce a blank page?" which I hope I've already answered.



Brian,
Thanks for your insights. You were correct in using image copy resize. That did the trick. I know have a working script that outputs the gif that i need.

CODE
<?php
resize("./", "http://images.wsdot.wa.gov/nwflow/flowmaps/sysvert.gif", "538", "./");
function resize($cur_dir, $cur_file, $newwidth, $output_dir)
{
    $dir_name = $cur_dir;
    $olddir = getcwd();
    $dir = opendir($dir_name);
    $filename = $cur_file;
    $format='image/gif';
    if(preg_match("/.jpg/i", "$filename"))
    {
        $format = 'image/jpeg';
    }
    if (preg_match("/.gif/i", "$filename"))
    {
        $format = 'image/gif';
    }
    if(preg_match("/.png/i", "$filename"))
    {
        $format = 'image/png';
    }
    if($format!='')
    {
        list($width, $height) = getimagesize($filename);
        $newheight=$height*$newwidth/$width;
        switch($format)
        {
            case 'image/jpeg':
            $source = imagecreatefromjpeg($filename);
            break;
            case 'image/gif';
            $source = imagecreatefromgif($filename);
            break;
            case 'image/png':
            $source = imagecreatefrompng($filename);
            break;
        }
        $dimg = imagecreatetruecolor(640,538);
        imagealphablending($dimg, false);
        $source = @imagecreatefromgif("$filename");
        imagecopyresized($dimg, $source, 0,0,0,438, $newwidth, $newheight, $width, $height);
        $filename="$output_dir/test1.gif";
        @imagegif($dimg,$filename);
    }
}
?>

User is offlinePM
Go to the top of the page
Toggle Multi-post QuotingQuote Post

Reply to this topicStart new topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

 



- Lo-Fi Version Time is now: 22nd November 2019 - 08:23 PM