Jump to content

adding multiple width & heights


acctman

Recommended Posts

i'm using this script to create thumbnails but I also need 2 other sizes. I know the easiest way to do this is just add $new_width and $new_height to the function at top and call it like

CreateSquareThumb(50,50,test.jpg,small.jpg,$border=1) is there a way to hard code all 3 file sizes into the function so that it does not have to be called 3 different times for each size?

 

function CreateSquareThumb($source,$dest,$border=1) {
    $new_width = 200;
    $new_height = 200;
    $sourcedate = 0;
    $destdate = 0;
    global $convert;
    if (file_exists($dest)) {
       clearstatcache();
       $sourceinfo = stat($source);
       $destinfo = stat($dest);
       $sourcedate = $sourceinfo[10];
       $destdate = $destinfo[10];
    }
    if (!file_exists($dest) or ($sourcedate > $destdate)) {
       global $ImageTool;
       $imgsize = GetImageSize($source);
       $width = $imgsize[0];
       $height = $imgsize[1];

    if ($width > $height) {
       $xoord = ceil(($width - $height) / 2 );
       $width = $height;
    } else {
       $yoord = ceil(($height - $width) / 2);
       $height = $width;
    }
       $new_im = ImageCreatetruecolor($new_width,$new_height);
       $im = imagecreatefromjpeg($source);
       imagecopyresampled($new_im,$im,0,0,$xoord,$yoord,$new_width,$new_height,$width,$height);
       imagejpeg($new_im,$dest,90);
    }
}

//self test
//CreateSquareThumb("/home/site/public_html/temp/test.jpg", "/home/site/public_html/temp/smalltest.jpg");

Link to comment
https://forums.phpfreaks.com/topic/154108-adding-multiple-width-heights/
Share on other sites

try

<?php
function CreateSquareThumb($source,$dest,$border=1) {
    $new_width = 200;
    $new_height = 200;
    $sizes = array( 
        array( 'width' =>200, 'height' =>200 ),
        array( 'width' =>150, 'height' =>150 ),
        array( 'width' =>100, 'height' =>100 )
    );
    $i = 0;
    foreach( $sizes as $size )
    {
        $sourcedate = 0;
        $destdate = 0;
        global $convert;
        if (file_exists($dest.$i)) {
           clearstatcache();
           $sourceinfo = stat($source);
           $destinfo = stat($dest.$i);
           $sourcedate = $sourceinfo[10];
           $destdate = $destinfo[10];
        }
        if (!file_exists($dest.$i) or ($sourcedate > $destdate)) {
           global $ImageTool;
           $imgsize = GetImageSize($source);
           $width = $imgsize[0];
           $height = $imgsize[1];

        if ($width > $height) {
           $xoord = ceil(($width - $height) / 2 );
           $width = $height;
        } else {
           $yoord = ceil(($height - $width) / 2);
           $height = $width;
        }
         $new_im = ImageCreatetruecolor($size['width'],$size['height']);
         $im = imagecreatefromjpeg($source);
         imagecopyresampled($new_im,$im,0,0,$xoord,$yoord,$size['width'],$size['height'],$width,$height);
         imagejpeg($new_im,$dest.$i,90);
       }
    }
}
?>

you could put your sizes in an array and loop through it.

$sizes=array(
array("width"=200,"height"=200), 
array("width"=10,"height"=10)
);  // etc etc etc

foreach($sizes as $size) 
{
          //$size['height'] is 200 and width is 200 
           // do whatever you need to do/
}

try

<?php
function CreateSquareThumb($source,$dest,$border=1) {
    $new_width = 200;
    $new_height = 200;
    $sizes = array( 
        array( 'width' =>200, 'height' =>200 ),
        array( 'width' =>150, 'height' =>150 ),
        array( 'width' =>100, 'height' =>100 )
    );
    $i = 0;
    foreach( $sizes as $size )
    {
        $sourcedate = 0;
        $destdate = 0;
        global $convert;
        if (file_exists($dest.$i)) {
           clearstatcache();
           $sourceinfo = stat($source);
           $destinfo = stat($dest.$i);
           $sourcedate = $sourceinfo[10];
           $destdate = $destinfo[10];
        }
        if (!file_exists($dest.$i) or ($sourcedate > $destdate)) {
           global $ImageTool;
           $imgsize = GetImageSize($source);
           $width = $imgsize[0];
           $height = $imgsize[1];

        if ($width > $height) {
           $xoord = ceil(($width - $height) / 2 );
           $width = $height;
        } else {
           $yoord = ceil(($height - $width) / 2);
           $height = $width;
        }
         $new_im = ImageCreatetruecolor($size['width'],$size['height']);
         $im = imagecreatefromjpeg($source);
         imagecopyresampled($new_im,$im,0,0,$xoord,$yoord,$size['width'],$size['height'],$width,$height);
         imagejpeg($new_im,$dest.$i,90);
       }
    }
}
?>

 

I tried that code, but only 1 thumbnail was created

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.