SitePoint Sponsor

User Tag List

Results 1 to 10 of 10
  1. #1
    SitePoint Addict tina88's Avatar
    Join Date
    Jan 2007
    Location
    UK
    Posts
    270
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Two image questions

    Hiya, I have two questions relating to the uploading of images.

    1. How do I create a thumbnail image with a maximum width of 150px and a maximum height of 100px? I tried using some code I found but it left horrible black borders on either side of the image.

    2. When I upload I want to resize the image to 600px wide and save it as imagenameTIMESTAMP.extension in the images folder. This is working fine, but I also want to resize that image again to be the thumbnail size and save that with the same filename, but in the images/thumb folder. Is this possible?

    Here is the code I have so far:
    PHP Code:
    function createResize($pathToImages,$thumbWidth) {
    // This is the temporary file created by PHP 
    $uploadedfile $_FILES['uploadfile']['tmp_name'];

    // Create an Image from it so we can do the resize
    $src imagecreatefromjpeg($uploadedfile);

    // Capture the original size of the uploaded image
    list($width,$height)=getimagesize($uploadedfile);

    // For our purposes, I have resized the image to be
    // whatever is defined in the function call
    $newwidth=$thumbWidth;
    $newheight=($height/$width)*$newwidth;
    $tmp=imagecreatetruecolor($newwidth,$newheight);

    // this line actually does the image resizing, copying from the original
    // image into the $tmp image
    imagecopyresampled($tmp,$src,0,0,0,0,$newwidth,$newheight,$width,$height); 

    // incoming filename 
    $name $_FILES['uploadfile']['name']; 
    // explode it to get the extention 
    $parts explode("."$name); 
    // add the timestamp 
    $filename $parts[0].time().'.'.$parts[1]; 

    // now write the resized image to disk. I have assumed that you want the
    // resized, uploaded image file to reside in the ./images subdirectory.
    $filename2 $pathToImages.$filename;
    imagejpeg($tmp,$filename2,100);

    imagedestroy($src);
    imagedestroy($tmp); // NOTE: PHP will clean up the temp file it created when the request
    // has completed

    To call this function to create the first image I use:
    PHP Code:
    echo createResize('images/','600px'); 
    Thanx, Tina

  2. #2
    SitePoint Enthusiast
    Join Date
    Jun 2007
    Location
    Ukraine
    Posts
    84
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Q1: You should create new image (new size) and copy content from old image to the new one.
    Q2: Yes, that is possible. Explain what makes problem for you.

  3. #3
    SitePoint Addict tina88's Avatar
    Join Date
    Jan 2007
    Location
    UK
    Posts
    270
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hiya, thanks for your quick reply. With the first question, I want it to be 150px wide and 100px high. I don't want the image to be distorted though. That is what I do not understand how to do. The second problem is how do I grad the filename that has just been created to enable me to resize the image? The timestamp part is automatically generated so I need to grab after the first image has been created. Tina

  4. #4
    SitePoint Enthusiast
    Join Date
    Jun 2007
    Location
    Ukraine
    Posts
    84
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Regarding question 1:

    PHP Code:
    $w_dest 150;
    $h_dest 100;
    $dest = @imagecreatetruecolor($w_dest,$h_dest) or die("Cannot Initialize new GD image stream");
    $white imagecolorallocate($dest255255255);
    imagefill($dest,1,1,$white);
    imagecopyresampled($dest$src0000$w_dest$h_dest$w_src$h_src); 
    Regarding question 2:
    PHP Code:
    imagejpeg($dest,'path1',100);
    imagejpeg($dest,'path2',100); 

  5. #5
    SitePoint Addict tina88's Avatar
    Join Date
    Jan 2007
    Location
    UK
    Posts
    270
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hiya, thanks for your help, but I am not surehow this will work. What I wanted to do was create two image the first being 600pixels wide and the second being 150pixels wide and 100pixels high. These two images will be created when the user submits a form.

    To avoid the problem of duplicate filenames a timestamp is added to the filename when it is saved. What I need is for both images to have the same name (and therefore timestamp) but saved in different folders.

    e.g. the 600pixel wide image = images/pic12345678.jpg where 12345678 is the timestamp assigned. The thumbnail image should be saved as images/thumb/pic12345678.jpg

    I think that maybe I did not explain very clearly before so I apologise

    Thanx, Tina

  6. #6
    SitePoint Addict tina88's Avatar
    Join Date
    Jan 2007
    Location
    UK
    Posts
    270
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    bump! Please help me

  7. #7
    derrrp
    Join Date
    Aug 2006
    Location
    earth
    Posts
    923
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi,

    This is probably pretty basic but works well for me. I'm sure you'll be able to change it accordingly to fit into your function/needs.

    PHP Code:
    $imgdir "path/to/images/";
    $thumbdir "path/to/thumb/";

    $file_name $_FILES['uploadfile']['name'];
    $file_name str_replace(" ""_"$file_name);
    $tmp_name $_FILES['uploadfile']['tmp_name'];

    $imgloc $imgdir $file_name;
    $thumbname str_replace(substr($file_name, -41), "_thmb."$file_name);
    $thmloc $thumbdir $thumbname;

    if(
    move_uploaded_file($tmp_name$imgloc))
    {
    copy($imgloc$thmloc);
    }
     
    //Do image resizing here
    $thmW 200;
    $thmH 150;
    $imgW 400;
    $imgH 300;
    //Get original image dimensions
    list($width$height) = getimagesize($imgloc);
    //Resample
    $img_p imagecreatetruecolor($imgW$imgH);
    $thm_p imagecreatetruecolor($thmW$thmH);
                
    $img_templ imagecreatefromjpeg($imgloc);
    $thm_templ imagecreatefromjpeg($thmloc);
                
    imagecopyresampled($img_p$img_templ0000$imgW$imgH$width$height);
    imagecopyresampled($thm_p$thm_templ0000$thmW$thmH$width$height);
                
    imagejpeg($img_p$imgloc65);
    imagejpeg($thm_p$thmloc30); 

    Hope that helps.
    No, I REALLY dislike having to use Joomla.

  8. #8
    derrrp
    Join Date
    Aug 2006
    Location
    earth
    Posts
    923
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Keep in mind, you'll probably want to verify that the file type is a jpeg before the above code is ran.
    No, I REALLY dislike having to use Joomla.

  9. #9
    SitePoint Addict tina88's Avatar
    Join Date
    Jan 2007
    Location
    UK
    Posts
    270
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you crowden I will have a play when I get home and let you know what I come up with. Tina

  10. #10
    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)
    Hey tina, it is my simple function which I hope, will fulfill your need lol!
    PHP Code:
    class ImageUpload{
        
        var 
    $ArrImageType;
        var 
    $picpath;
        var 
    $thumbpath;
        var 
    $maxwidth;
        var 
    $thumbwidth;
        var 
    $thumbheight;

        var 
    $tablename;
        var 
    $imagefieldname;
        
        function 
    __construct(){
            
    $this->ArrImageType = array('image/gif''image/jpeg''image/jpg''image/pjpeg''image/png');
        }
        function 
    Create_ThumbNail($ImageType$rW$rH$imagename$do="thumbnail"){
            
    $full_picmain $this->picpath "/" $imagename;
            if(
    $do != "thumbnail")
                
    $tsrc $this->picpath "/$imagename";
            else
                
    $tsrc $this->thumbpath "/$imagename";
            
    # 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);
            }        
        }
        function 
    Upload($PicFile){
            if(
    is_uploaded_file($PicFile['tmp_name'])){
                if(
    $PicFile['size'] < 1048576 && in_array($PicFile['type'], $this->ArrImageType)){
                    
    $imgExt $this->GetImageExtention($PicFile['type']);
                    
    $PicName substr(md5(time()), rand(026), 6) . "." $imgExt;
                    
    $PicFullPathName $this->picpath "/" $PicName;
                    if(
    move_uploaded_file($PicFile['tmp_name'], $PicFullPathName)){
                        
    $this->Create_ThumbNail($PicFile['type'], 10080$PicName"thumbnail"); # Create thumbnail image 
                        
    list($width$height) = getimagesize($PicFullPathName);
                        if(
    $width $this->maxwidth){ # default size.
                            
    $ratio = (($width $this->maxwidth) * 100) / $width;
                            
    $newheight $height - ($ratio $height 100);
                            
    $this->Create_ThumbNail($PicFile['type'], $this->maxwidth$newheight$PicName"resize"); # resizing the image
                        
    }
                        
    mysql_query("INSERT INTO " $this->tablename " SET " $this->imagefieldname "='$PicName'") or die(mysql_error());
                    }
                }
            }
    # is picture uploaded
        
    }
        
    # 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";
        }
    }
    $obj = new ImageUpload;

    $obj->maxwidth             600;                        # Maximum large image size
    $obj->thumbwidth         150;                        # Thumbnail width
    $obj->thumbheight         150;                        # Thumbnail height
    $obj->picpath             './uploads';                 # Large image path
    $obj->thumbpath         './uploads/thumbnails';     #Thumbnail path

    $obj->tablename         "tblproductimages";        #table name
    $obj->imagefieldname     "ProductImagePath";        #field name that stores imagename

    $PicFile $_FILES['PrdImg'];

    $obj->Upload($PicFile); 
    Feel free to ask if you do not understand or any need.
    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
  •