|
בעיה עם הקטנת תמונות (משהו ממש מוזר)
שלום,
כתבתי סקריפט להקטנת תמונות (בעזרת דקל, ולאחר מכן הפך למדריך קטן בארכיון ).
אם מקטינים בעזרת GD תמונות GIF, התמונה המוקטנת יוצאת מפוקסלת ומגעילה. מאחר ואני צריך להקטין גם קבצי GIF, החלטתי להמיר את הGIF המקורי לתמונת JPG זמנית, לפתוח אותה, ואז לשמור כGIF שוב.
מה שמוזר הוא הדבר הבא: כשאני מקטין לגודל של רוחב 60 וגובה פרופורציונאלי לרוחב הזה (לפי ערכי התמונה הספיציפית), הכל מצוין. כל תמונה שאני לא אשים תוקטן באופן מושלם. בדקתי כבר כמה וכמה תמונות GIF שקופות, לא שקופות וכו' (עם קבצי JPG אין בעיה בכלל בכל מקרה).
כשאני מקטין לגודל של רוחב 300 וגובה פרופורציונאלי, תמונות GIF בלי שקיפות וJPG יוצאות בסדר גמור, אבל תמונות GIF עם שקיפות יוצאות לפעמים עם רקע שחור. אין לי שמץ של מושג למה.
הסקריפט:
קוד PHP:
// ************************ // התחלת סקריפט הקטנת תמונה // ************************ $extention=substr($file,strrpos($file,".")+1,strlen($file)-strrpos($file,".")); $simplename=substr($file,0,strrpos($file,".")); $simplename=$simplename . "_small"; // שינוי שם הקובץ לאותיות קטנות בלבד $simplename=strtolower($simplename); $extention=strtolower($extention); $dbname=$simplename . "." . $extention; // התאמת מימדים חדשים $new_width=300; $new_height=(round(($height/$width)*$new_width)); // Resample if ($extention=="jpg" || $extention=="jpeg") { $image_p = imagecreatetruecolor($new_width, $new_height); } elseif ($extention=="gif") { $image_p = imagecreate($new_width, $new_height); } imagefill($image_p, 0, 0, 0xFFFFFF); if ($extention=="jpg" || $extention=="jpeg") { $image = imagecreatefromjpeg($path . $file); } elseif ($extention=="gif") { $image = imagecreatefromgif($path . $file); } if ($extention=="gif") { // שמירת הצבע השקוף בתמונה $transcolor=imagecolortransparent($image); if ($transcolor != -1) { // לקיחת נתוני אדום ירוק כחול מהצבע השקוף $ar = imagecolorsforindex($image,$transcolor); $new_trans = imagecolorallocatealpha($image_p,$ar[0],$ar[1],$ar[2],$ar[3]); // החזרת הצבע השקוף המקורי אל התמונה לאחר ההקטנה imagecolortransparent($image_p,$new_trans); } } // הקטנת התמונה imagecopyresampled($image_p, $image, $dst_x, $dst_y, 0, 0, $new_width, $new_height, $width, $height); // Output $filepath=$path . "small/" . $dbname; $filepath2=$path . "/small/temp.jpg"; if ($extention=="jpg" || $extention=="jpeg") { imagejpeg($image_p,$filepath,95); } elseif ($extention=="gif") { imagejpeg($image_p,$filepath2,100); imagedestroy($image); imagedestroy($image_p); $image_p = imagecreatetruecolor($new_width,$new_height); imagefill($image_p, 0, 0, 0xFFFFFF); $image = imagecreatefromjpeg($filepath2); imagecopymerge($image_p, $image, 0, 0, 0, 0, $new_width,$new_height, 100); imagegif($image_p,$filepath); unlink($filepath2); } imagedestroy($image); imagedestroy($image_p); // ********************** // סוף סקריפט הקטנת תמונה // **********************
הניחו שwidth ו-height כבר נתונים, כמו גם file ועוד משתנים.
המון תודה !
בר 
|