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
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);
       }
    }
}
?>

Link to comment
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);
       }
    }
}
?>

 

I tried that code, but only 1 thumbnail was created

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.