לוגו אתר Fresh          
 
 
  אפשרות תפריט  ראשי     אפשרות תפריט  צ'אט     אפשרות תפריט  מבזקים     אפשרות תפריט  צור קשר     חץ שמאלה ‎print ‎"Hello World!"; if‎ ‎not rules.‎know ‎then rules.‎read(); חץ ימינה  

לך אחורה   לובי הפורומים > מחשבים > תכנות ובניית אתרים
שמור לעצמך קישור לדף זה באתרי שמירת קישורים חברתיים
תגובה
 
כלי אשכול חפש באשכול זה



  #2  
ישן 11-11-2007, 13:41
  יובל_כ יובל_כ אינו מחובר  
 
חבר מתאריך: 19.03.07
הודעות: 75
קח
בתגובה להודעה מספר 1 שנכתבה על ידי alkolombra שמתחילה ב "שמירת תמונה בגדלים שונים... אפשרי ?"

זה הקוד שאני משתמש בו באחד האתרים.

הוא מאפשר מעלה תמונות לשרת (בגישת FTP) ומקטין אותן לשני גדלים. שים לב שיש אופציה להקטנת תמונה ושמירת פרופורציות או להקטנה וגזירה (כלומר מתקבלת תמונה מוקטנת אבל בגודל קבוע). ההקטנה השניה מאפשרת שימוש ב tumbnails זהים בגודל למרות שתמונות המקור בפרופורציות שונות. דוגמה - פייסבוק.

תיעוד והערות על גבי הקוד. הפונקציה גם פותחת ונועלת את התיקיות של התמונות.





קוד PHP:
 /////////////////////////////////////////////////////////////////////////////////
function upload_file(&$newfilename)
{
    if (!(
$_FILES['userfile']))
        return 
S_NO_DATA;
 
    
$message '';
    
$FILE_EXTS '';
    
$newfilename '';
    
$destination_folder '';
    
$destination_file '';
    
$MAX_FILE_SIZE 300 1000// 300 KB
    
$FILE_EXTS = array('.jpg''.jpeg''.png''.gif');
    
$MAX_PICT_X 600;
    
$MAX_PICT_Y 600;
    
$MAX_TUMB_X 100;
    
$MAX_TUMB_Y 100;
    
$local_file $_FILES['userfile']['tmp_name'];
 
    
$fileOrigName basename($_FILES['userfile']['name']);
    
$file_ext strtolower(substr($fileOrigName,strrpos($fileOrigN  ame,".")));
    
// Create new file name
    
$newfilename time() .$file_ext;
    if (!
in_array($file_ext$FILE_EXTS))
    {
        
$message "קובץ לא חוקי. הקובץ חייב להיות מהטיפוסים הבאים בלבד: ";
        
$message .= implode(" - "$FILE_EXTS);
        return 
$message;
    }
    
// Path for File Upload (relative to login dir)
    
$tmp_file 'tmp/' .$newfilename;
    
$tmp_ftpfile "public_html/dat/" .$tmp_file;
 
    
// Connect to FTP Server
    
$conn_id ftp_connect(FTP_SERVER);
 
    
// Login to FTP Server
    
$login_result ftp_login($conn_idFTP_USER_NAMEFTP_USER_PASS);
 
    
// VERIFY Log In Status
    
if ((!$conn_id) || (!$login_result))
        return 
"לא נוצר קשר לשרת. הקובץ לא הועלה." ." file upload - " .__LINE__;
    
// UPLOAD the file
    
$upload ftp_put($conn_id$tmp_ftpfile$local_fileFTP_BINARY);
     
// VERIFY Upload Status
    
if (!$upload)
        return 
"תקלת תקשורת, הקובץ לא הועלה לשרת." ." file upload - " .__LINE__;
    
// Reduce Image Size to max sizes
    
$dest_file "tmp/s_" .$newfilename;
    
$retval reduceImageSize($tmp_file$dest_file$MAX_PICT_X$MAX_PICT_Y);
    if (
S_OK != $retval)
        return 
$retval ."הקטנת תמונה נכשלה " ." file upload - " .__LINE__;
 
    
//File Size Check
    
if (filesize($dest_file) > $MAX_FILE_SIZE)
    {
        
unlink($tmp_file); // delete the tmp file
        
unlink($dest_file); // delete the new file
        
return "הקובץ גדול מדי, לא ניתן לטעון קבצים גדולים מ " .$MAX_FILE_SIZE ."KB";
    }
    
// create tumbnail pict
    
$tmb_file "tmp/t_" .$newfilename;
    
$retval reduceImageSize($tmp_file$tmb_file$MAX_TUMB_X$MAX_TUMB_YKEEP_SIZE);
    if (
S_OK != $retval)
        return 
$retval ."הקטנת תמונה נכשלה " ." file upload - " .__LINE__;
    
// delete the original tmp file
    
unlink($tmp_file);
 
    
$destination_folder "public_html/dat/pict";
    
$destination_file 'pict/' .$newfilename// relative to THIS place
    // UNLOCK the destination folder
    
if (ftp_chmod($conn_id0777$destination_folder) === false
    {
        
unlink($dest_file); // delete the tmp file
        
return "לא ניתן להעביר את הקובץ למיקום הסופי" ." file upload - " .__LINE__;
    }
    
// MOVE the PICT file to its destination
    
if (!rename($dest_file$destination_file))
        return 
"הקובץ לא נשמר עקב תקלת מערכת flhndler " .__LINE__;
    
// LOCK the destination folder
    
if (ftp_chmod($conn_id0555$destination_folder) === false
     return 
"לא ניתן להעביר את הקובץ למיקום הסופי" ." file upload - " .__LINE__;
 
    
$destination_folder "public_html/dat/tmbs";
    
$destination_file 'tmbs/' .$newfilename// relative to THIS place
    // UNLOCK the destination folder
    
if (ftp_chmod($conn_id0777$destination_folder) === false
    {
        
unlink($tmb_file); // delete the tmp file
        
return "לא ניתן להעביר את הקובץ למיקום הסופי" ." file upload - " .__LINE__;
    }
    
// MOVE the TUMBNAIL file to its destination
    
if (!rename($tmb_file$destination_file))
        return 
"הקובץ לא נשמר עקב תקלת מערכת flhndler " .__LINE__;
    
// LOCK the destination folder
    
if (ftp_chmod($conn_id0555$destination_folder) === false
     return 
"לא ניתן להעביר את הקובץ למיקום הסופי" ." file upload - " .__LINE__;
 
    
ftp_close($conn_id); // Close the FTP Connection
 
    
return S_OK;
}
/////////////////////////////////////////////////////////////////////////////////////////
function reduceImageSize($orig_file$dest_file$max_x$max_y$trim KEEP_PROPORTIONS
{
 
    
$image_info getImageSize($orig_file);
    switch (
$image_info['mime']) {
        case 
'image/gif':
            if (
imagetypes() & IMG_GIF) { // not the same as IMAGETYPE
                
$o_im imageCreateFromGIF($orig_file);
            } else {
                
$ermsg 'הקטנת GIF אינה נתמכת בשרת';
            }
            break;
        case 
'image/jpeg':
            if (
imagetypes() & IMG_JPG) {
                
$o_im imageCreateFromJPEG($orig_file);
            } else {
                
$ermsg 'הקטנת JPG אינה נתמכת בשרת';
            }
            break;
        case 
'image/png':
            if (
imagetypes() & IMG_PNG) {
                
$o_im imageCreateFromPNG($orig_file);
            } else {
                
$ermsg 'הקטנת PNG אינה נתמכת בשרת';
            }
            break;
        case 
'image/wbmp':
            if (
imagetypes() & IMG_WBMP) {
                
$o_im imageCreateFromWBMP($orig_file);
            } else {
                
$ermsg 'הקטנת BMP אינה נתמכת בשרת';
            }
            break;
        default:
            
$ermsg $image_info['mime'].' סוג תמונה אינו נתמך בשרת';
            break;
    }
 
    if (!isset(
$ermsg)) {
        
$orig_x imagesx($o_im) ;
        
$orig_y imagesy($o_im) ;
 
        
$dst_x 0;
        
$dst_y 0;
        
$src_x 0;
        
$src_y 0;
        
$dst_w 0;
        
$dst_h 0;
        
$src_w $orig_x;
        
$src_h $orig_y;
        
// calculate the image size limitations
        
if (($orig_x $max_x) && ($orig_y $max_y)) { // NO SCALE !!!
            
$dst_w $orig_x
            
$dst_h $orig_y 
        }
        else if (
$orig_y $max_y) {        // need to scale only X
            
$dst_w $max_x
            
$dst_h round($dst_w $orig_y $orig_x);
        }
        else if (
$orig_x $max_x) {        // need to scale only Y
            
$dst_h $max_y
            
$dst_w round($dst_h $orig_x $orig_y);
        }
        else {                             
// need to scale X and Y
            
if ($trim == KEEP_PROPORTIONS)
            {
                
$dst_w round($max_y $orig_x $orig_y); // limit according to max_y and proportion
                
if ($dst_w $max_x){
                    
$dst_w $max_x;                         // limit according to max_x
                
}
                
$dst_h round($dst_w $orig_y $orig_x); // limit according to max_x and proportion
                
if ($dst_h $max_y){
                    
$dst_h $max_y;                         // limit according to max_y
                    
$dst_w round($dst_h $orig_x $orig_y);
                }
            }
            elseif(
$trim == KEEP_SIZE)
            {
                
$dst_w $max_x;
                
$dst_h $max_y;
 
                if ( (
$orig_x $orig_y) > )
                { 
// width is more the height
                    
$src_w $src_h;
                    
$src_x round( ($orig_x $src_w) / 2);
                }
                else
                { 
// height is more then width
                    
$src_h $src_w;
                    
$src_y round( ($orig_y $src_h) / 2);
                }
            }
            else{
                return 
'unknown resize option';
            }
        }
        
$t_im imageCreateTrueColor($dst_w$dst_h);
 
        
imagecopyresampled$t_im$o_im$dst_x$dst_y$src_x$src_y$dst_w$dst_h$src_w$src_h );
        
$resOK false;
 
        switch (
$image_info['mime']) {
            case 
'image/gif':
                
$resOK imagegif($t_im$dest_file);
                break;
            case 
'image/jpeg':
                
$resOK imagejpeg($t_im$dest_file);
                break;
            case 
'image/png':
                
$resOK imagepng($t_im$dest_file);
                break;
            case 
'image/wbmp':
                
$resOK imagewbmp($t_im$dest_file);
                break;
            default:
                
$ermsg $image_info['mime'].' שמירת תמונה מוקטנת נכשלה ' .$orig_file;
                break;
        }
 
        if (!
$resOK)
            return 
$ermsg 'יצירת תמונה נכשלה - ' .$orig_file;
        
imageDestroy($o_im);
        
imageDestroy($t_im);
    }
    return isset(
$ermsg)? $ermsg S_OK;



_____________________________________
-----------
פרילאנס PHP.
yuval.karmi@gmail.com

תגובה ללא ציטוט תגובה עם ציטוט חזרה לפורום
תגובה

כלי אשכול חפש באשכול זה
חפש באשכול זה:

חיפוש מתקדם
מצבי תצוגה דרג אשכול זה
דרג אשכול זה:

מזער את תיבת המידע אפשרויות משלוח הודעות
אתה לא יכול לפתוח אשכולות חדשים
אתה לא יכול להגיב לאשכולות
אתה לא יכול לצרף קבצים
אתה לא יכול לערוך את ההודעות שלך

קוד vB פעיל
קוד [IMG] פעיל
קוד HTML כבוי
מעבר לפורום



כל הזמנים המוצגים בדף זה הם לפי איזור זמן GMT +2. השעה כעת היא 22:45

הדף נוצר ב 0.29 שניות עם 12 שאילתות

הפורום מבוסס על vBulletin, גירסא 3.0.6
כל הזכויות לתוכנת הפורומים שמורות © 2025 - 2000 לחברת Jelsoft Enterprises.
כל הזכויות שמורות ל Fresh.co.il ©

צור קשר | תקנון האתר