www.webdeveloper.com
Results 1 to 2 of 2

Thread: resizing (uploaded) images

  1. #1
    Join Date
    Jan 2005
    Posts
    66

    resizing (uploaded) images

    Okay, I have my image script that's finally working. Now I'd like to resize the uploaded image (permanatly). I'd assume this would be done during the uploading process.

    Basically, I'd give preset porportions. The uploaded image would then be resized to the correct size I specified, then saved to the server.

    How should I go about doing this?

  2. #2
    Join Date
    Jan 2005
    Location
    Alicante (Spain)
    Posts
    7,740
    Here's my de facto resizing function. It covers most needs. Resizes most image types and maintains transparency and alpha if the image has them.
    PHP Code:
    function resize($source$destination null$w 125$h 125$quality 100)
    {
        
    $details = @getimagesize($source) or die("I cannot open $source");
        
    $type preg_replace('@^.+(?<=/)(.+)$@''$1'$details['mime']);
        eval(
    '$source = imagecreatefrom'.$type.'($source);');
        if(
    $details[0] < $details[1])
        {
            
    $w round(($h $details[1]) * $details[0]);
        }
        else
        {
            
    $h round(($w $details[0]) * $details[1]);
        }
        if(
    imageistruecolor($source))
        {
            
    $slate = @imagecreatetruecolor($w$h) or die('Invalid thumbnail dimmensions');
            
    imageAlphaBlending($slatefalse);
            
    imageSaveAlpha($slatetrue);
        }
        else
        {
            
    $slate = @imagecreate($w$h) or die('Invalid thumbnail dimmensions');
            if(
    false !== ($trans = @imagecolorsforindex($sourceimagecolortransparent($source))))
            {
                
    $trans ImageColorAllocate($slate$trans['red'], $trans['green'], $trans['blue']);
                
    imagefilledrectangle($slate00$w 1$h 1$trans);
                
    imagecolortransparent($slate$trans);
            }
        }
        
    imagecopyresampled($slate$source0000$w$h$details[0], $details[1]);
        
    $destination or header('Content-Type: '.$details['mime']);
        eval(
    '@image'.$type.'($slate'.(($type=='jpeg')?',$destination,$quality':($destination?',$destination  ':'')).');');
        
    imagedestroy($source);
        
    imagedestroy($slate);
        
    $destination or die;

    If you have trouble integrating this you will need to post your code but before you do, check out the code in this thread.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles