[6] | 1 | <?php |
---|
| 2 | |
---|
| 3 | class data_images extends data |
---|
| 4 | { |
---|
| 5 | public $image_error = ""; |
---|
| 6 | public $image_message = ""; |
---|
| 7 | |
---|
| 8 | function get_image_error() { return $this->image_error; } |
---|
| 9 | function get_image_message() { return $this->image_message; } |
---|
| 10 | |
---|
| 11 | function img_size($file, $max_width, $max_height) |
---|
| 12 | { $img_infos = getimagesize($file); |
---|
| 13 | if($img_infos) |
---|
| 14 | { if($img_infos[0] > $max_width || $img_infos[1] > $max_height) |
---|
| 15 | { $r = $max_width / $img_infos[0]; |
---|
| 16 | if($r * $img_infos[1] > $max_height) $r = $max_height / $img_infos[1]; |
---|
| 17 | $img_size = array |
---|
| 18 | ( "width" => floor($r * $img_infos[0]), |
---|
| 19 | "height" => floor($r * $img_infos[1]) |
---|
| 20 | ); |
---|
| 21 | } |
---|
| 22 | else $img_size = array |
---|
| 23 | ( "width" => $img_infos[0], |
---|
| 24 | "height" => $img_infos[1] |
---|
| 25 | ); |
---|
| 26 | return $img_size; |
---|
| 27 | } |
---|
| 28 | return false; |
---|
| 29 | } |
---|
| 30 | |
---|
| 31 | function unique_file_name($dest, $file_name, $prefix = "img_") |
---|
| 32 | { $dest .= strlen($dest) > 0 && substr($dest, -1) != "/" ? "/" : ""; |
---|
| 33 | if(is_dir($dest)) |
---|
| 34 | { $ext = ""; |
---|
| 35 | if(strpos($file_name, ".") !== false) |
---|
| 36 | { $v_ext_path = explode(".", $file_name); |
---|
| 37 | $ext = ".".$v_ext_path[count($v_ext_path) - 1]; |
---|
| 38 | } |
---|
| 39 | $i = 0; |
---|
| 40 | while(file_exists($dest.$prefix.$i.$ext)) $i++; |
---|
| 41 | return $prefix.$i.$ext; |
---|
| 42 | } |
---|
| 43 | return false; |
---|
| 44 | } |
---|
| 45 | |
---|
| 46 | function upload_image($image, $dest, $img_file = null) |
---|
| 47 | { switch($_FILES[$image]["error"]) |
---|
| 48 | { case UPLOAD_ERR_OK : break; |
---|
| 49 | default : $this->image_error = "Impossible d'uploader l'image."; |
---|
| 50 | } |
---|
| 51 | if(!$this->image_error) |
---|
| 52 | { $img_file = isset($img_file) ? $img_file : $_FILES[$image]["name"]; |
---|
| 53 | $v_name = explode(".", $img_file); |
---|
| 54 | $ext = $v_name[count($v_name) - 1]; |
---|
| 55 | if |
---|
| 56 | ( strcasecmp($ext, "png") == 0 || |
---|
| 57 | strcasecmp($ext, "gif") == 0 || |
---|
| 58 | strcasecmp($ext, "jpg") == 0 || |
---|
| 59 | strcasecmp($ext, "jpeg") == 0 |
---|
| 60 | ) |
---|
| 61 | { if(file_exists($dest)) |
---|
| 62 | { if(!is_dir($dest)) $this->image_error = "le dossier d'upload est un fichier. Impossible d'y uploader l'image"; |
---|
| 63 | } |
---|
| 64 | else |
---|
| 65 | { @mkdir($dest); |
---|
| 66 | clearstatcache(); |
---|
| 67 | if(!file_exists($dest) && !is_dir($dest)) $this->image_error = "Impossible de creer le dossier d'upload"; |
---|
| 68 | } |
---|
| 69 | if(!$this->image_error) |
---|
| 70 | { if |
---|
| 71 | ( @move_uploaded_file |
---|
| 72 | ( $_FILES[$image]["tmp_name"], |
---|
| 73 | $dest."/".$img_file |
---|
| 74 | ) !== false |
---|
| 75 | ) return $img_file; |
---|
| 76 | else $this->image_error = "Impossible de copier l'image uploadee"; |
---|
| 77 | } |
---|
| 78 | } |
---|
| 79 | else $this->image_message = "Le fichier image doit être au format png, gif ou jpg"; |
---|
| 80 | } |
---|
| 81 | return false; |
---|
| 82 | } |
---|
| 83 | |
---|
| 84 | function img_thumb($src, $max_width, $max_height, $thumbs_dir = "") |
---|
| 85 | { if(strlen($thumbs_dir) > 0) |
---|
| 86 | { if(!@is_dir($thumbs_dir)) @mkdir($thumbs_dir); |
---|
| 87 | if(!@is_dir($thumbs_dir)) return false; |
---|
| 88 | } |
---|
| 89 | $thumb = array(); |
---|
| 90 | try |
---|
| 91 | { $sql = |
---|
| 92 | "SELECT *" |
---|
| 93 | ." FROM #--thumbs" |
---|
| 94 | ." WHERE src=".$this->eq($src) |
---|
| 95 | ." AND max_width=".$this->eq($max_width) |
---|
| 96 | ." AND max_height=".$this->eq($max_height); |
---|
| 97 | $rst = $this->sql->query($sql); |
---|
| 98 | if($v_rst = $this->sql->fetch_assoc($rst)) $thumb = $v_rst; |
---|
| 99 | $this->sql->free_result($rst); |
---|
| 100 | } |
---|
| 101 | catch(Exception $_e) { $thumb = false; } |
---|
| 102 | if($thumb !== false) |
---|
| 103 | { if($thumb) return $thumb; |
---|
| 104 | $thumbs_dir .= strlen($thumbs_dir) > 0 && substr($thumbs_dir, -1) != "/" ? "/" : ""; |
---|
| 105 | $thumb_dir = $max_width."x".$max_height."/"; |
---|
| 106 | if(!@is_dir($thumbs_dir.$thumb_dir)) @mkdir($thumbs_dir.$thumb_dir); |
---|
| 107 | if(!@is_dir($thumbs_dir.$thumb_dir)) return false; |
---|
| 108 | $thumb_file = $thumb_dir.$this->unique_file_name($thumbs_dir.$thumb_dir, $src, "img_"); |
---|
| 109 | if($thumb_file === false) return false; |
---|
| 110 | if(($thumb = $this->make_thumb($src, $max_width, $max_height, $thumbs_dir, $thumb_file)) !== false) |
---|
| 111 | { try |
---|
| 112 | { $sql = |
---|
| 113 | "INSERT INTO #--thumbs" |
---|
| 114 | ."(src, src_width, src_height, max_width, max_height, thumb_file, thumb_width, thumb_height, creation_date) VALUES" |
---|
| 115 | ."( ".$this->eq($thumb["src"]) |
---|
| 116 | .", ".$this->eq($thumb["src_width"]) |
---|
| 117 | .", ".$this->eq($thumb["src_height"]) |
---|
| 118 | .", ".$this->eq($max_width) |
---|
| 119 | .", ".$this->eq($max_height) |
---|
| 120 | .", ".$this->eq($thumb["thumb_file"]) |
---|
| 121 | .", ".$this->eq($thumb["thumb_width"]) |
---|
| 122 | .", ".$this->eq($thumb["thumb_height"]) |
---|
| 123 | .", ".$this->eq($thumb["creation_date"]) |
---|
| 124 | .")"; |
---|
| 125 | $rst = $this->sql->query($sql); |
---|
| 126 | return $thumb; |
---|
| 127 | } |
---|
| 128 | catch(Exception $_e) {} |
---|
| 129 | } |
---|
| 130 | } |
---|
| 131 | return false; |
---|
| 132 | } |
---|
| 133 | |
---|
| 134 | function make_thumb($src, $max_width, $max_height, $thumbs_dir, $thumb_file, $quality = null) |
---|
| 135 | { $dest = $thumbs_dir.$thumb_file; |
---|
| 136 | $size = getimagesize($src); |
---|
| 137 | if($size[0]) |
---|
| 138 | { if($size[0] > $size[1]) |
---|
| 139 | { $width = $max_width; |
---|
| 140 | $height = ($size[1] * ($width / $size[0])); |
---|
| 141 | } |
---|
| 142 | else |
---|
| 143 | { $height = $max_height; |
---|
| 144 | $width = $size[0] * ($height / $size[1]); |
---|
| 145 | } |
---|
| 146 | $v_ext_path = explode(".", $src); |
---|
| 147 | $ext = $v_ext_path[count($v_ext_path) - 1]; |
---|
| 148 | if(strcasecmp($ext, "jpg") == 0 || strcasecmp($ext, "jpeg") == 0) $ext = "jpg"; |
---|
| 149 | elseif(strcasecmp($ext, "gif") == 0) $ext = "gif"; |
---|
| 150 | elseif(strcasecmp($ext, "png") == 0) $ext = "png"; |
---|
| 151 | else $ext = ""; |
---|
| 152 | $create_function = ""; |
---|
| 153 | $thumb_function = ""; |
---|
| 154 | if(strcasecmp($ext, "jpg") == 0) |
---|
| 155 | { $create_function = "imagecreatefromjpeg"; |
---|
| 156 | $thumb_function = "imagejpeg"; |
---|
| 157 | } |
---|
| 158 | elseif(strcasecmp($ext, "gif") == 0) |
---|
| 159 | { $create_function = "imagecreatefromgif"; |
---|
| 160 | $thumb_function = "imagegif"; |
---|
| 161 | $quality = NULL; |
---|
| 162 | } |
---|
| 163 | elseif(strcasecmp($ext, "png") == 0) |
---|
| 164 | { $create_function = "imagecreatefrompng"; |
---|
| 165 | $thumb_function = "imagepng"; |
---|
| 166 | } |
---|
| 167 | if($create_function) |
---|
| 168 | { $src_img = $create_function($src); |
---|
| 169 | $thumb_img = imagecreatetruecolor($width, $height); |
---|
| 170 | imagecopyresampled($thumb_img, $src_img, 0, 0, 0, 0, $width, $height, $size[0], $size[1]); |
---|
| 171 | if(isset($quality)) $thumb_img = $thumb_function($thumb_img, $dest, $quality); |
---|
| 172 | else $thumb_img = $thumb_function($thumb_img, $dest); |
---|
| 173 | if($thumb_img !== false) |
---|
| 174 | { return array |
---|
| 175 | ( "src" => $src, |
---|
| 176 | "src_width" => $size[0], |
---|
| 177 | "src_height" => $size[1], |
---|
| 178 | "max_width" => $max_width, |
---|
| 179 | "max_height" => $max_height, |
---|
| 180 | "thumb_file" => $thumb_file, |
---|
| 181 | "thumb_width" => $width, |
---|
| 182 | "thumb_height" => $height, |
---|
| 183 | "creation_date" => date("Y-m-d H:i:s") |
---|
| 184 | ); |
---|
| 185 | } |
---|
| 186 | } |
---|
| 187 | } |
---|
| 188 | return false; |
---|
| 189 | } |
---|
| 190 | |
---|
| 191 | } |
---|
| 192 | |
---|
| 193 | ?> |
---|