Kod funkcji zmniejszania zdjęć:
// Zmniejszanie do odpowiedniej wielkości function resize($filef, $new_width, $new_height, $thumb, $quality) { if ($info === false) return false; $img = null; $img = imagecreatefromjpeg($filef); $th = imagecreatetruecolor($new_width, $new_height); imagecopyresampled($th, $img, 0, 0, 0, 0, $new_width, $new_height, $info[0], $info[1]); imagejpeg($th, $filef, $quality); imagedestroy($th); imagedestroy($img); return true; } //tworzenie miniaturki 80x80 function resize2($filef, $new_width, $new_height, $thumb, $quality) { if ($info === false) return false; $img = null; $img = imagecreatefromjpeg($filef); $th = imagecreatetruecolor(80, 80); imagecopyresampled($th, $img, 0, 0, 0, 0, $new_width, $new_height, $info[0], $info[1]); imagejpeg($th, $filef, $quality); imagedestroy($th); imagedestroy($img); return true; }
Ogólny skrypt upload'u zdjęć:
function adpic($id,$arg,$key,$position) { $path="../files/images/".$id; if ($info[0]>$info[1]) { if ($info[0]>625) { $height=(625/$info[0])*$info[1]; resize($path."/".$newimage, 625, $height, $path."/".$newimage, 75); } } elseif ($info[0]<$info[1]) { if ($info[1]>450) { $width=(450*$info[0])/$info[1]; resize($path."/".$newimage, $width, 450, $path."/".$newimage, 75); } } if ($info[0]<=$info[1]) { $thumb_height = (80/$info[0])*$info[1]; resize2($path."/thumb_".$newimage, 80, $thumb_height, $path."/thumb_".$newimage, 80); } elseif ($info[0]>$info[1]) { $thumb_width=(80*$info[0])/$info[1]; resize2($path."/thumb_".$newimage, $thumb_width, 80, $path."/thumb_".$newimage, 80); } }
Skrypt wywoływania funkcji:
foreach ($_FILES["image"]["error"] as $key => $error) { $position=$position+1; if ($_FILES["image"]["name"][$key]!="") { adpic($id,image,$key,$position); } }
W przypadku gdy skrypt się sypie wyskakują błędy o niezidentyfikowanych zmiennych a także "Invalid argument supplied for foreach()" (pewnie ze względu, że te zmienne nie są przesyłane).