PHP Developers Network

A community of PHP developers offering assistance, advice, discussion, and friendship.
 
Loading
It is currently Tue Sep 29, 2020 3:36 am

All times are UTC - 5 hours




Post new topic Reply to topic  [ 4 posts ] 
Author Message
 Post subject: scale image down
PostPosted: Wed Oct 26, 2005 2:58 pm 
Offline
DevNet Master
User avatar

Joined: Tue Dec 28, 2004 6:57 pm
Posts: 2745
Location: Tallinn, Estonia


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 27, 2005 1:55 pm 
Offline
DevNet Master
User avatar

Joined: Tue Dec 28, 2004 6:57 pm
Posts: 2745
Location: Tallinn, Estonia
ok i got some stuff kicking. PROBLEMS! i have probably had 1 too many today so i am sucking hard at math but this is what i got right now

Syntax: [ Download ] [ Hide ]
function createImage($image_path,$image_name,$max_width = 80,$max_height = 60)

    {

        $src_img = imagecreatefromjpeg($image_path.'/'.$image_name);



        $height = imagesy($src_img);

        $width  = imagesx($src_img);



        if ($height <= $max_height && $width <= $max_width)

            return true;

       

        if ($width > $height)

            $top = 'width';

        else

            $top = 'height';



        if ($top == 'height')

        {

            $new_h  = $max_height;

            $diff   = $height-$new_h;

            $new_w  = $max_width-$diff;

            $dst_img = imagecreatetruecolor($new_w,$new_h);

            imagecopyresized($dst_img,$src_img,0,0,0,0,$new_w,$new_h,imagesx($src_img),imagesy($src_img));



            imagejpeg($dst_img, $image_path/$image_name, 70);

            return true;

        }



        if ($top == 'width')

        {

            $new_w = $max_width;

            $diff  = $width-$new_w;

            $new_h  = $max_height-$diff;

            $dst_img = imagecreatetruecolor($new_w,$new_h);

            imagecopyresized($dst_img,$src_img,0,0,0,0,$new_w,$new_h,imagesx($src_img),imagesy($src_img));



            imagejpeg($dst_img, $image_path/$image_name, 70);

            return true;

        }

    }


only works on jpegs obviously but right now thats all i care about. what i really need is the math stuff to get the new height and new width while keeping the same proportions. really the problem is at

Syntax: [ Download ] [ Hide ]
$new_h  = $max_height;

$diff   = $height-$new_h;

$new_w  = $max_width-$diff;



//and



$new_w = $max_width;

$diff  = $width-$new_w;

$new_h  = $max_height-$diff;


i hope you see what i mean. thanks


Top
 Profile  
 
 Post subject:
PostPosted: Fri Oct 28, 2005 10:18 am 
Offline
Briney Mod
User avatar

Joined: Mon Jan 19, 2004 7:11 pm
Posts: 6446
Location: 53.01N x 112.48W
This should work:

Syntax: [ Download ] [ Hide ]
//first, we need to figure out which dimension is the largest

$width_change = $max_width/$img_width;

$height_change = $max_height/$img_height;



//if (the amount the image width is greater than the max width) is greater than

//(the amount the image height is greater than the max height), use

//the change in width to determine height

if($width_change < $height_change)

{

    $new_width = $max_width;

    $new_height = $img_height * $width_change;

}

//likewise with the height



else

{

     $new_height = $max_height;

     $new_width = $img_width * $height_change;

}


This is untested and could probably be optimized a bit, but I've used this logic before and it's sound

_________________
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.


Top
 Profile  
 
 Post subject:
PostPosted: Fri Oct 28, 2005 12:41 pm 
Offline
DevNet Master
User avatar

Joined: Tue Dec 28, 2004 6:57 pm
Posts: 2745
Location: Tallinn, Estonia


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 4 posts ] 

All times are UTC - 5 hours


Who is online

Users browsing this forum: No registered users and 6 guests


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 post attachments in this forum

Jump to:  
Powered by phpBB® Forum Software © phpBB Group