image.php (1547B)
1 <?php 2 class ModelToolImage extends Model { 3 public function resize($filename, $width, $height) { 4 if (!is_file(DIR_IMAGE . $filename) || substr(str_replace('\\', '/', realpath(DIR_IMAGE . $filename)), 0, strlen(DIR_IMAGE)) != str_replace('\\', '/', DIR_IMAGE)) { 5 return; 6 } 7 8 $extension = pathinfo($filename, PATHINFO_EXTENSION); 9 10 $image_old = $filename; 11 $image_new = 'cache/' . utf8_substr($filename, 0, utf8_strrpos($filename, '.')) . '-' . $width . 'x' . $height . '.' . $extension; 12 13 if (!is_file(DIR_IMAGE . $image_new) || (filemtime(DIR_IMAGE . $image_old) > filemtime(DIR_IMAGE . $image_new))) { 14 list($width_orig, $height_orig, $image_type) = getimagesize(DIR_IMAGE . $image_old); 15 16 if (!in_array($image_type, array(IMAGETYPE_PNG, IMAGETYPE_JPEG, IMAGETYPE_GIF))) { 17 return DIR_IMAGE . $image_old; 18 } 19 20 $path = ''; 21 22 $directories = explode('/', dirname($image_new)); 23 24 foreach ($directories as $directory) { 25 $path = $path . '/' . $directory; 26 27 if (!is_dir(DIR_IMAGE . $path)) { 28 @mkdir(DIR_IMAGE . $path, 0777); 29 } 30 } 31 32 if ($width_orig != $width || $height_orig != $height) { 33 $image = new Image(DIR_IMAGE . $image_old); 34 $image->resize($width, $height); 35 $image->save(DIR_IMAGE . $image_new); 36 } else { 37 copy(DIR_IMAGE . $image_old, DIR_IMAGE . $image_new); 38 } 39 } 40 41 if ($this->request->server['HTTPS']) { 42 return HTTPS_CATALOG . 'image/' . $image_new; 43 } else { 44 return HTTP_CATALOG . 'image/' . $image_new; 45 } 46 } 47 }