SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    Pragmatic Programmer halfasleeps's Avatar
    Join Date
    Feb 2006
    Location
    Altoona, PA. USA
    Posts
    1,945
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    change image size when saving image?

    i have 3 diiferent images I upload for each new product.

    they are usually the same image just different sizes

    so i was wondering if there was a way i could just upload 1 image and save it as 3 different images and 3 different sizes?


    thanks.
    Altoona Design
    Freelance Flex developer for hire.
    ActionScript Programmer with 8 Years Experience.

  2. #2
    rajug.replace('Raju Gautam'); bronze trophy Raju Gautam's Avatar
    Join Date
    Oct 2006
    Location
    Kathmandu, Nepal
    Posts
    4,013
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Here is my function for this:
    PHP Code:
        function Create_ThumbNail($ImageType$ImagePath$rW$rH$img$do="thumbnail"){
            
    $full_picmain $ImagePath "/" $img;
            if(
    $do != "thumbnail")
                
    $tsrc "$ImagePath/$img";
            else
                
    $tsrc "$ImagePath/thumbnails/$img";
            
    ////////////// Starting of GIF thumb nail creation///////////
            
    if($ImageType == "image/gif"){
                
    $im imagecreatefromgif($full_picmain);
                
    $width imagesx($im);                // Original picture width is stored
                
    $height imagesy($im);                // Original picture height is stored
                
    if($width $rW){$n_width $rW;}
                else{
    $n_width $width;}
                if(
    $height $rH){$n_height $rH;}
                else{
    $n_height $height;}
                
    $newimage imagecreatetruecolor($n_width,$n_height);
                
    imagecopyresized($newimage$im0000$n_width$n_height$width,$height);
                if(
    function_exists("imagegif")) {
                    
    header("Content-type: image/gif");
                    
    imagegif($newimage$tsrc);
                }
                elseif(
    function_exists("imagejpeg")) {
                    
    header("Content-type: image/jpeg");
                    
    imagejpeg($newimage,$tsrc);
                }
                
    chmod("$tsrc",0777);
            }
            
    ////// starting of JPG thumb nail creation//////////
            
    if($ImageType == "image/pjpeg" || $ImageType == "image/jpeg" || $ImageType == "image/jpg"){
                
    $im imagecreatefromjpeg($full_picmain);
                
    $width imagesx($im);              // Original picture width is stored
                
    $height imagesy($im);             // Original picture height is stored
                
    if($width $rW){$n_width $rW;}
                else{
    $n_width $width;}
                if(
    $height $rH){$n_height $rH;}
                else{
    $n_height $height;}
                
    $newimage imagecreatetruecolor($n_width,$n_height);                 
                
    imagecopyresized($newimage$im0000$n_width$n_height$width,$height);
                
    imagejpeg($newimage$tsrc);
                
    chmod("$tsrc",0777);
            }
            
    ////// starting of JPG thumb nail creation//////////
            
    if($ImageType == "image/png"){
                
    $im imagecreatefrompng($full_picmain); 
                
    $width imagesx($im);              // Original picture width is stored
                
    $height imagesy($im);             // Original picture height is stored
                
    if($width $rW){$n_width $rW;}
                else{
    $n_width $width;}
                if(
    $height $rH){$n_height $rH;}
                else{
    $n_height $height;}
                
    $newimage imagecreatetruecolor($n_width,$n_height);                 
                
    imagecopyresized($newimage$im0000$n_width$n_height$width,$height);
                
    imagepng($newimage$tsrc);
                
    chmod("$tsrc",0777);
            }        
        }
        
    # Get image extention #
        
    function GetImageExtention($type){
            if(
    $type == "image/jpeg" || $type == "image/jpg" || $type == "image/pjpeg")
                return 
    "jpg";
            elseif(
    $type "image/png")
                return 
    "png";
            elseif(
    $type == "image/gif")
                return 
    "gif";
        } 
    You can call this function like this:
    PHP Code:
    $PicFile $_FILES['PrdImg'];
    for(
    $i 0$i 10$i++){
        if(
    is_uploaded_file($PicFile['tmp_name'])){
            if(
    $PicFile['size'] < 1048576 && in_array($PicFile['type'], $obj->ArrImageType)){
                
    $imgExt $obj->GetImageExtention($PicFile['type']);
                
    $PicName "prjpic_" substr(md5(time()), 04) . "." $imgExt;
                
    $PicFullPathName $picpath "/" $PicName;
                if(
    move_uploaded_file($PicFile['tmp_name'], $PicFullPathName)){
                    
    $obj->Create_ThumbNail($PicFile['type'], $picpath10080$PicName"thumbnail"); //alternate option for thumbnail is resize
                    
    list($width,$height) = getimagesize($PicFullPathName);
                    if(
    $width 640){ //default size.
                        
    $ratio = (($width 640) * 100) / $width;
                        
    $newheight $height - ($ratio $height 100);
                        
    $obj->Create_ThumbNail($PicFile['type'], $picpath640$newheight$PicName"resize"); //resizing the image
                    
    }
                    
    mysql_query("UPDATE tblproductimages SET ProductImagePath='$PicName' WHERE PictureID='$newpicid'") or die(mysql_error());
                }
            }
        }
    //is picture uploaded
    }//for 
    I have these functions in a lirbary function that's why i have used object to call the function. I think this will help you.

    Feel free to ask if you could not get it.
    Mistakes are proof that you are trying.....
    ------------------------------------------------------------------------
    PSD to HTML - SlicingArt.com | Personal Blog | ZCE - PHP 5


Bookmarks

Posting Permissions

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