Hi Guys,

I have this thumbnail script, but I want to set the width to 200px wide and the height constrained automaticically to the width.

Can anyone please help:

<? 
header("Content-type: image/jpeg"); 
Header("Content-type: image/png"); 

$new_width=200;  //Image width Change if needed 
$new_height=200;  //Image height  THIS NEEDS TO BE MODIFIED 

$source_path="pics/";   //Source File path 
$destination_path="small/";     //Destination file path 

$db = mysql_connect("localhost", "*********","**********") or die("Cannot Connect");   // Database Connection 

mysql_select_db("sleaz_green",$db) or die("Cannot open database"); //Database Name 

$sql = mysql_query("SELECT * FROM **********") or die("Query failed");  //Query  

while ($row = mysql_fetch_array($sql)) 
 {  

$image_name = $row["url"];  //Image path retrived 

//Identifying Image type 

$len = strlen($image_name); 
$pos =strpos($image_name,"."); 
$type = substr($image_name,$pos + 1,$len); 

if ( $type=="jpeg" || $type=="jpg") 
{ 
    thumb_jpeg ($image_name); //Call to jpeg function 
} 
else if($type="png" || $type="PNG") 
{ 
    thumb_png ($image_name);    //Call to PNG function 
} 

echo "<b>Done........</b>"; 

} 

// JPEG function
function thumb_jpeg($image_name) {
    global $source_path, $destination_path, $new_width, $new_height;

$destimg = imagecreatetruecolor($new_width, $new_height) or die('Problem creating image');
$srcimg = imagecreatefromjpeg($source_path . $image_name) or die('Problem opening source image');
imagecopyresampled($destimg, $srcimg, 0, 0, 0, 0, $new_width, $new_height, imagesx($srcimg), imagesy($srcimg)) or die('Problem resizing image');
imagejpeg($destimg, $destination_path . $image_name) or die('Problem saving image');
}

// PNG function
function thumb_png($image_name) {
    global $source_path, $destination_pth, $new_width, $new_height;

$destimg = imagecreatetruecolor($new_width, $new_height) or die('Problem creating image');
$scrimg = imagecreatefrompng($source_path . $image_name) or die('Problem opening source image');
imagecopyresampled($destimg, $srcimg, 0, 0, 0, 0, $new_width, $new_height, imagesx($srcimg), imagesy($srcimg)) or die('Problem resizing');
imagepng($destimg, $destination_path . $image_name) or die('Problem saving image');
}
?>

    all you need is some basic math....

    800x600 -> 200x$X = Y1(X2/X1) or 600(200/800) which is 600*.25 which is 150 so you get a image thats 200x150 from 800x600....

      Eeek! :bemused:

      Where do I put that in my above code to make that work then?

      Thanks

        7724 wrote:

        Eeek! :bemused:

        Where do I put that in my above code to make that work then?

        Thanks

        I personally would put it in afunction and call it for where you need the new height for your new images.... using the current imagesy() to get the right ratio

          Thanks tekky,

          But that's totally gone over my head. This script is from an online tutorial (far too complex for me at this early stage of learning php)

          I have no idea where to put the function you posted in my script and how it interatcts with the other functions there.

          Thanks

            We are not here to modify your script we are here to answer to your questions and give u hints, so maybe u should ask tekky, how much will cost me to make me that change? :rolleyes: :rolleyes: :rolleyes:

              7724 wrote:

              Thanks tekky,

              But that's totally gone over my head. This script is from an online tutorial (far too complex for me at this early stage of learning php)

              I have no idea where to put the function you posted in my script and how it interatcts with the other functions there.

              Thanks

              Consider this a freebie then, you really should TRY though since trying is what will get you the furthest in both learning and in seeking help

              <?
              header("Content-type: image/jpeg");
              Header("Content-type: image/png");
              
              function reduceH($curW,$curH)
              {
                return $curH*(200/$curW);
              }
              
              $new_width=200;  //Image width Change if needed
              $new_height=200;  //Image height  THIS NEEDS TO BE MODIFIED
              
              $source_path="pics/";   //Source File path
              $destination_path="small/";     //Destination file path
              
              $db = mysql_connect("localhost", "*********","**********") or die("Cannot Connect");   // Database Connection
              
              mysql_select_db("sleaz_green",$db) or die("Cannot open database"); //Database Name
              
              $sql = mysql_query("SELECT * FROM **********") or die("Query failed");  //Query  
              
              while ($row = mysql_fetch_array($sql))
              {  
              
              $image_name = $row["url"];  //Image path retrived
              
              //Identifying Image type
              
              $len = strlen($image_name);
              $pos =strpos($image_name,".");
              $type = strtolower(substr($image_name,$pos + 1,$len));
              
              if ( $type=="jpeg" || $type=="jpg")
              {
                  thumb_jpeg ($image_name); //Call to jpeg function
              }
              else if($type="png")
              {
                  thumb_png ($image_name);    //Call to PNG function
              }
              
              echo "<b>Done........</b>";
              
              }
              
              // JPEG function
              function thumb_jpeg($image_name) {
                  global $source_path, $destination_path, $new_width, $new_height;
              
              $destimg = imagecreatetruecolor($new_width, $new_height) or die('Problem creating image');
              $srcimg = imagecreatefromjpeg($source_path . $image_name) or die('Problem opening source image');
              $new_height = reduceH(imagesx($srcimg),imagesx($srcimg));
              imagecopyresampled($destimg, $srcimg, 0, 0, 0, 0, $new_width, $new_height, imagesx($srcimg), imagesy($srcimg)) or die('Problem resizing image');
              imagejpeg($destimg, $destination_path . $image_name) or die('Problem saving image');
              }
              
              // PNG function
              function thumb_png($image_name) {
                  global $source_path, $destination_pth, $new_width, $new_height;
              
              $destimg = imagecreatetruecolor($new_width, $new_height) or die('Problem creating image');
              $scrimg = imagecreatefrompng($source_path . $image_name) or die('Problem opening source image');
              $new_height = reduceH(imagesx($srcimg),imagesx($srcimg));
              imagecopyresampled($destimg, $srcimg, 0, 0, 0, 0, $new_width, $new_height, imagesx($srcimg), imagesy($srcimg)) or die('Problem resizing');
              imagepng($destimg, $destination_path . $image_name) or die('Problem saving image');
              }
              ?>
              

              That should do it....

                bogu wrote:

                We are not here to modify your script we are here to answer to your questions and give u hints, so maybe u should ask tekky, how much will cost me to make me that change? :rolleyes: :rolleyes: :rolleyes:

                and maybe you should rethink what you are saying when it concerns me.... and apologize for being rude to 7724

                  I dont like when people come here paste there code and ask for someone to change it for them, as I was saying I'm here only to answer question and give hint, but u are right too, I shouldnt come here and say that, so I'm sorry ...

                    The admonition from bogyou notwithstanding, here's some code along with a (hopefully helpful) explanation:

                    Although using "imagesy()", as tekky suggested, will work fine, the function call for that would need to be placed inside each of the "thumb_*()" functions, since it requires a GD resource as an argument.

                    I'd suggest using "getimagesize()" (which takes the source pathname) instead. Then the other functions won't need to be altered, and you could use a separate function just for figuring the new height.

                    Since the height needs to be computed for each image, you'd have to place the new function's call inside the "while" loop, and pass to it the source image's pathname and the new width. Right after the "$image_name = $row["url"];" line would be a good place. The function itself can be placed about anywhere; I'd put it at the bottom of the script.

                    Call the function like this:

                    while ($row = mysql_fetch_array($sql)) {   
                    $image_name = $row["url"]; //Image path retrived $new_height = new_height($source_path . $image_name, $new_width); if ($new_height === false) { continue; // On error, skip and go to next image }// the rest of your script

                    The function:

                    function new_height($name, $new_wd)
                    {
                        $image_size = getimagesize($name);
                        $old_wd = $image_size[0];
                        $old_ht = $image_size[1];
                        $new_ht = round($new_wd / $old_wd) * $old_ht;
                        return $new_ht;
                    }
                      Write a Reply...