PHP Developers Network

A community of PHP developers offering assistance, advice, discussion, and friendship.
 
Loading
It is currently Tue Aug 11, 2020 10:33 pm

All times are UTC - 5 hours




Post new topic Reply to topic  [ 5 posts ] 
Author Message
 Post subject: resize images
PostPosted: Wed Oct 05, 2005 10:09 pm 
Offline
Forum Newbie

Joined: Wed Oct 05, 2005 9:54 pm
Posts: 3
Jcart | Please use
Syntax: [ Download ] [ Hide ]
and
Syntax: [ Download ] [ Hide ]
tags where appropriate when posting code. Read:  </span>





I am accepting images into my script and then resizing them to create both a feature image (400x300px) and a thumbnail (120x90px).  When creating the feature image, all goes well until an image comes in less than 400x300.  At this time, I just create the feature image with the original w and h.  This looks okay, but I would like the image to be 400x300.  So, my question is, is there a way to put a image that is, say 96x72, onto an image that is 400x300 without changing the image proportions?  In other words, the picture would remian the original size (96x72), but would be centered on an 400x300 image with a white background.



Here is my current function:
Syntax: [ Download ] [ Hide ]
function createFeature($dirname,$img,$w,$h,$i)

{

   $imagedata = getimagesize($img);

   

   if($imagedata[0]>$w && $imagedata[1]>$h){

     if ($w && ($imagedata[0] < $imagedata[1]))

     {

       $w = ($h / $imagedata[1]) * $imagedata[0];

     }

     else

     {

       $h = ($w / $imagedata[0]) * $imagedata[1];

     }

   } else {

     $w=$imagedata[0];

     $h=$imagedata[1];

   }

   echo "<br>img = ".$img;

   echo "<br>w = ".$w;

   echo "<br>h = ".$h;

   echo "<br>imagedata[0] = ".$imagedata[0];

   echo "<br>imagedata[1] = ".$imagedata[1];

   



   $im2 = ImageCreateTrueColor($w,$h);

   $image = ImageCreateFromJpeg($img);

   imagecopyResampled ($im2, $image, 0, 0, 0, 0, $w, $h, $imagedata[0], $imagedata[1]);

   ImageJpeg($im2, "$dirname\pic.jpg", 70);

}



Jcart | Please use
Syntax: [ Download ] [ Hide ]
and
Syntax: [ Download ] [ Hide ]
tags where appropriate when posting code. Read:  </span>


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 06, 2005 10:05 am 
Offline
Briney Mod
User avatar

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

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


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 06, 2005 3:22 pm 
Offline
Forum Newbie

Joined: Wed Oct 05, 2005 9:54 pm
Posts: 3
thanks Pickle!! it worked great!

Here is the modified code for anyone trying to do this:

Syntax: [ Download ] [ Hide ]
<?php

function createFeature($dirname,$img,$w,$h,$i)

{

   $dx=$dy=0;

   $imagedata = getimagesize($img);

   

   if($imagedata[0]>$w && $imagedata[1]>$h){

     if ($w && ($imagedata[0] < $imagedata[1]))

     {

       $dw = ($h / $imagedata[1]) * $imagedata[0];

     }

     else

     {

       $dh = ($w / $imagedata[0]) * $imagedata[1];

     }

   } else {

         $dx=(0.5*$w)-(0.5*$imagedata[0]);

         $dy=(0.5*$h)-(0.5*$imagedata[1]);

         $dw=$imagedata[0];

         $dh=$imagedata[1];



   }



   $im2 = ImageCreateTrueColor($w,$h);

   imagefill($im2, 0, 0, 0xFFFFFF);

   $image = ImageCreateFromJpeg($img);

   imagecopyResampled ($im2, $image, $dx, $dy, 0, 0, $dw, $dh, $imagedata[0], $imagedata[1]);

   ImageJpeg($im2, "$dirname\picture0.jpg", 70);

}      

?>


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 06, 2005 3:26 pm 
Offline
Forum Newbie

Joined: Thu Oct 06, 2005 3:15 pm
Posts: 2
Hey would you mind posting your working code? I'm in the need for a similar function. All of you code php tags and all would be great. thanks


Top
 Profile  
 
 Post subject:
PostPosted: Thu Oct 06, 2005 9:38 pm 
Offline
Forum Newbie

Joined: Wed Oct 05, 2005 9:54 pm
Posts: 3
Here is the code that works correctly. I had to modify it a little more.

Syntax: [ Download ] [ Hide ]
function createFeature($dirname,$img,$w,$h)

{

   $dx=$dy=0;

   $imagedata = getimagesize($img);

   

   if($imagedata[0]>$w && $imagedata[1]>$h){

     if ($w && ($imagedata[0] < $imagedata[1]))

     {

       $dw = ($h / $imagedata[1]) * $imagedata[0];

           $dh=$h;

     }

     else

     {

       $dh = ($w / $imagedata[0]) * $imagedata[1];

           $dw=$w;

     }

   } else {

         $dx=(0.5*$w)-(0.5*$imagedata[0]);

         $dy=(0.5*$h)-(0.5*$imagedata[1]);

         $dw=$imagedata[0];

         $dh=$imagedata[1];



   }





   $im2 = ImageCreateTrueColor($w,$h);

   imagefill($im2, 0, 0, 0xFFFFFF);

   $image = ImageCreateFromJpeg($img);

   imagecopyResampled ($im2, $image, $dx, $dy, 0, 0, $dw, $dh, $imagedata[0], $imagedata[1]);

   ImageJpeg($im2, "$dirname\picture0.jpg", 70);

}


Just call this function with a directory path, the image path, and the width and height you want to make the new image to be.

So, a sample call might look like this:

Syntax: [ Download ] [ Hide ]
createFeature("mysite/images","c:\my_docs\images\vaction_pic.jpg",400,300);


Hope that helps
:D


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

All times are UTC - 5 hours


Who is online

Users browsing this forum: No registered users and 5 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