Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New Coder
    Join Date
    Jul 2008
    Posts
    13
    Thanks
    2
    Thanked 0 Times in 0 Posts

    RESOLVED: Problem with a .jpeg extension where .jpg works OK in a PHP/ Mysql gallery

    Hi,

    I am having a problem with uploading files .jpeg extensions to a PHP/Mysql gallery. The .jpg extension work but not the .jpeg extension.

    To rectify the problem I tried first to replace the reference to the .jpg extension to .jpeg in my 'functions.php' file to see if that would work. However, the result of that was that the script uploaded the image with the file extension '.jjpeg' (yes, thats a double 'j').

    Below is the functions.php file BEFORE I tried the above.

    Thanks a mil,

    Paul

    PHP Code:
    function uploadImage($inputName$uploadDir)
    {
        
    $image     $_FILES[$inputName];
        
    $imagePath '';
        
    $thumbnailPath '';
        
        
    // if a file is given
        
    if (trim($image['tmp_name']) != '') {
            
    $ext substr(strrchr($image['name'], "."), 1); 

            
    // generate a random new file name to avoid name conflict
            // then save the image under the new file name
            
    $imagePath md5(rand() * time()) . ".$ext";
            
    $result    move_uploaded_file($image['tmp_name'], $uploadDir $imagePath);
                
            if (
    $result) {
                
    // create thumbnail
                
    $thumbnailPath =  md5(rand() * time()) . ".$ext";
                
    $result createThumbnail($uploadDir $imagePath$uploadDir 'thumbnail/' $thumbnailPathTHUMBNAIL_WIDTHTHUMBNAIL_HEIGHT);
                
                
    // create thumbnail failed, delete the image
                
    if (!$result) {
                    
    unlink($uploadDir $imagePath);
                    
    $imagePath $thumbnailPath '';
                } else {
                    
    $thumbnailPath $result;
                }    
            } else {
                
    // the image cannot be uploaded
                
    $imagePath $thumbnailPath '';
            }
            
        }

        
        return array(
    'image' => $imagePath'thumbnail' => $thumbnailPath);
    }

    /*
        Create a thumbnail of $srcFile and save it to $destFile.
        The thumbnail will be $width pixels.
    */
    function createThumbnail($srcFile$destFile$width$height$quality 75)
    {
        
    $thumbnail '';
        
        if (
    file_exists($srcFile)  && isset($destFile))
        {
            
    $size        getimagesize($srcFile);
            
    $w           number_format($width0',''');
            
    $h           number_format($height0',''');
            
            
    $thumbnail =  copyImage($srcFile$destFile$w$h$quality);
        }
        
        
    // return the thumbnail file name on sucess or blank on fail
        
    return basename($thumbnail);
    }



    //Function to create the album thumbnails
    function createThumbnail2($srcFile$destFile$width$height$quality 75)
    {
        
    $thumbnail '';
        
        if (
    file_exists($srcFile)  && isset($destFile))
        {
            
    $size        getimagesize($srcFile);
            
    $w           number_format($width0',''');
            
    $h           number_format($height0',''');
            
            
    $thumbnail =  copyImage($srcFile$destFile$w$h$quality);
        }
        
        
    // return the thumbnail file name on sucess or blank on fail
        
    return basename($thumbnail);
    }




    /*
        Copy an image to a destination file. The destination
        image size will be $w X $h pixels
    */
    function copyImage($srcFile$destFile$w$h$quality 75)
    {
        
    $tmpSrc     pathinfo(strtolower($srcFile));
        
    $tmpDest    pathinfo(strtolower($destFile));
        
    $size       getimagesize($srcFile);

        if (
    $tmpDest['extension'] == "gif" || $tmpDest['extension'] == "jpg")
        {
           
    $destFile  substr_replace($destFile'jpg', -3);
           
    $dest      imagecreatetruecolor($w$h);
           
    //imageantialias($dest, TRUE);
        
    } elseif ($tmpDest['extension'] == "png") {
           
    $dest imagecreatetruecolor($w$h);
           
    //imageantialias($dest, TRUE);
        
    } else {
          return 
    false;
        }

        switch(
    $size[2])
        {
           case 
    1:       //GIF
               
    $src imagecreatefromgif($srcFile);
               break;
           case 
    2:       //JPEG
               
    $src imagecreatefromjpeg($srcFile);
               break;
           case 
    3:       //PNG
               
    $src imagecreatefrompng($srcFile);
               break;
           default:
               return 
    false;
               break;
        }

        
    imagecopyresampled($dest$src0000$w$h$size[0], $size[1]);

        switch(
    $size[2])
        {
           case 
    1:
           case 
    2:
               
    imagejpeg($dest,$destFile$quality);
               break;
           case 
    3:
               
    imagepng($dest,$destFile);
        }
        return 
    $destFile;


    Last edited by onoffpaul; 07-17-2008 at 05:50 PM. Reason: Resolved

  • #2
    Regular Coder ohgod's Avatar
    Join Date
    Jun 2008
    Location
    Ohio
    Posts
    579
    Thanks
    6
    Thanked 69 Times in 69 Posts
    the reason you were getting jjpeg is because of the substr_replace below. it moves back 3 characters and drops jpg in. so if you take the filename whatever.jpeg, move back 3 characters from the and and drop in jpeg, you'd get whatever.jjpeg

    PHP Code:
    if ($tmpDest['extension'] == "gif" || $tmpDest['extension'] == "jpg"
        { 
           
    $destFile  substr_replace($destFile'jpg', -3); 
    just add another condition for $tmpDest['extension'] == "jpeg" (which actually, just glancing at it i don't see where $tmpDest['extension'] is being set..) and change the starting point in your substr_replace statement.

  • #3
    New Coder
    Join Date
    Jul 2008
    Posts
    13
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Thanks a mil!


  •  

    Posting Permissions

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