SitePoint Sponsor

User Tag List

Results 1 to 11 of 11
  1. #1
    Always learning kigoobe's Avatar
    Join Date
    May 2004
    Location
    Paris
    Posts
    1,565
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Must be a simple question, nut how to upload resized images to server?

    Hi guys

    This is how I am storing an image to database

    PHP Code:
        $allowed_typesz = array("image/jpg""image/jpeg""image/pjpeg",  "image/png""image/x-png"); 
        
    $uploadfilez=$_FILES['auth_img']['tmp_name'];
        
    $uploadnamez=$_FILES['auth_img']['name'];
        
    $uploadtypez=$_FILES['auth_img']['type'];
        if(!
    in_array($uploadtypez$allowed_typesz)) {
        print 
    '<p>&nbsp;</p><p>&nbsp;</p><div class="mybutton2">
            <font color="red">'
    .$whatphoto.'<br /><br />
                    '
    .$please.' <a href="javascript: history.go(-1)">'.$goback.'</a> '.$whatphoto1.'</font></div>'
                include 
    'include/bottom.php';
                exit(); 
            }
        
    // RESIZE IMAGE TO BETTER DIMENSIONS AND SIZE 
            
    $src ImageCreateFromString(file_get_contents($uploadfilez)); // bitmap image works with all image types 
            
    $new_width 300// set max limit for width for new image 
            
    $new_height 300// set max limit for height for new image 
            
    $old_width ImageSx($src); //find width of old file 
            
    $old_height ImageSy($src); //find height of old file 
            
    if (($old_height $new_height)||($old_width $new_width)) {
                 if (
    $old_height $old_width)
                    {  
    $new_width round(($new_height $old_height) * $old_width); }
                if (
    $old_width $old_height)
                 { 
    $new_height round(($new_width $old_width) * $old_height); }
                } else
                { 
    $new_width $old_width;
                  
    $new_height $old_height;
                }
            
    $dst ImageCreateTrueColor($new_width,$new_height); //create new image 
            
    ImageCopyResampled($dst$src,0,0,0,0,$new_width,$new_height,$old_width,$old_height); // copy old image into new image resized 
            
    Imagedestroy($src); 
            
    $quality 75 //new file quality scale 0-100 (100 best) 
            
    ob_start(); 
            if (
    $uploadtypez=='image/gif'
            {
    Imagegif($dst$uploadfile);} 
            elseif (
    $uploadtypez=='image/png'
            {
    Imagepng($dst$uploadfilez);} else { 
            
    Imagejpeg($dst''$quality); } 
            
    $filedataz ob_get_clean(); 
            
    Imagedestroy($dst); 
            
    ///////////////////////////////////////////////////////////// 
            
    $uploadsizez strlen($filedataz);
            
    $img_height=$new_height;
            
    $img_width=$new_width;
            
    $filedataz=addslashes($filedataz); 
    Now I want to upload the resized image to my server, instead of the database. Question is, what should be uploaded?

    Had the resizing script were not present, I could simply upload $uploadfilez to the server. Or, I can upload $filedataz to a mysql base, after resizing.

    What should I upload to the server after resizing?
    Thanks for your time.

  2. #2
    ✯✯✯ silver trophybronze trophy php_daemon's Avatar
    Join Date
    Mar 2006
    Posts
    5,284
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    If you specified a file name for imagejpeg(), it'd save your image to a file:
    PHP Code:
    Imagejpeg($dst'path/to/image.jpg'$quality); 
    Saul

  3. #3
    Always learning kigoobe's Avatar
    Join Date
    May 2004
    Location
    Paris
    Posts
    1,565
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks, you mean, I don't need to upload that image to the server seperately, that line of code does this itself?

  4. #4
    Always learning kigoobe's Avatar
    Join Date
    May 2004
    Location
    Paris
    Posts
    1,565
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Edit; Yeah, that's working very nicely. Now, what about gif and png images?

    Thanks for your input.

  5. #5
    ✯✯✯ silver trophybronze trophy php_daemon's Avatar
    Join Date
    Mar 2006
    Posts
    5,284
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    There's no difference with gif and png from jpeg. Use imagegif ang imagepng respectively.
    Saul

  6. #6
    Always learning kigoobe's Avatar
    Join Date
    May 2004
    Location
    Paris
    Posts
    1,565
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks.

    Another thing. I am using this code, where though the first upload is working, the second one is not working. Any guess? Thanks again.

    PHP Code:
    $dst ImageCreateTrueColor($new_width,$new_height); //create new image 
    $dst2 ImageCreateTrueColor($new_width2,$new_height2); //create new image 
    ImageCopyResampled($dst$src,0,0,0,0,$new_width,$new_height,$old_width,$old_height); // copy old image into new image resized 
    ImageCopyResampled($dst2$src,0,0,0,0,$new_width2,$new_height2,$old_width,$old_height); // copy old image into new image resized 
    Imagedestroy($src); 
    $quality 90 //new file quality scale 0-100 (100 best) 
    ob_start(); 
    if(
    Imagejpeg($dst$path.$name$quality)) {
        
    $doc 1;

    if(
    Imagejpeg($dst2$path2.$name2$quality)) {
        
    $doc2 1;

    $filedata ob_get_clean(); 
    Imagedestroy($dst); 
    Imagedestroy($dst2); 

  7. #7
    ✯✯✯ silver trophybronze trophy php_daemon's Avatar
    Join Date
    Mar 2006
    Posts
    5,284
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Make sure $path2.$name2 gives you a right file path.
    Saul

  8. #8
    Always learning kigoobe's Avatar
    Join Date
    May 2004
    Location
    Paris
    Posts
    1,565
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    yeah, that I have checked, looks the path is OK.

  9. #9
    ✯✯✯ silver trophybronze trophy php_daemon's Avatar
    Join Date
    Mar 2006
    Posts
    5,284
    Mentioned
    2 Post(s)
    Tagged
    0 Thread(s)
    Also make sure the permissions are right on the directory.
    Saul

  10. #10
    Always learning kigoobe's Avatar
    Join Date
    May 2004
    Location
    Paris
    Posts
    1,565
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    done, it was a chmod problem for $path2 (i created a new folder here and forgot to change the chmod from 755 to 777)

  11. #11
    Always learning kigoobe's Avatar
    Join Date
    May 2004
    Location
    Paris
    Posts
    1,565
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanks a lot.


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
  •