Пример #1
0
 /**
  * Get thumbnail image
  * @param  string  $src  --- image url
  * @param  integer $w    --- width
  * @param  integer $h    --- height
  * @param  integer $q    --- quality
  * @param  boolean $crop --- crop or no
  * @return string        --- thumbnail url
  */
 public static function getImageThumb($src, $w = 600, $h = 250, $q = 95, $crop = true)
 {
     $thumb_url = $src;
     $ext = EmailFormBox::getExtension($src);
     if (!$ext) {
         return $thumb_url;
     }
     $cache = md5($src . "{$w}-{$h}-{$q}-{$crop}-gc");
     $uploads = wp_upload_dir();
     $cache_dir = $uploads["basedir"] . "/2014/00";
     $thumb_url = $uploads["baseurl"] . "/2014/00/{$cache}.{$ext}";
     $thumb_dir = $cache_dir;
     $thumb_file = $thumb_dir . "/{$cache}.{$ext}";
     if (!dir($cache_dir)) {
         mkdir($cache_dir, 0744, true);
     }
     if (!is_file($thumb_file)) {
         $editor = wp_get_image_editor($src);
         if (!is_wp_error($editor)) {
             $editor->resize($w, $h, $crop);
             $editor->set_quality($q);
             $editor->save($thumb_file);
         } else {
             $thumb_url = $src;
         }
     }
     return $thumb_url;
 }