public function pixum($width = 150, $height = false)
 {
     $cache_folder = media_base_path() . 'pixum' . DS;
     if ($height) {
         $h = $height;
     } else {
         $h = $width;
     }
     $h = intval($h);
     $w = intval($width);
     if ($h == 0) {
         $h = 1;
     }
     if ($w == 0) {
         $w = 1;
     }
     $extension = ".png";
     $hash = 'pixum-' . $h . 'x' . $w;
     $cachefile = normalize_path($cache_folder . DS . $hash . $extension, false);
     if (!file_exists($cachefile)) {
         $dirname_file = dirname($cachefile);
         if (!is_dir($dirname_file)) {
             mkdir_recursive($dirname_file);
         }
         $img = imagecreatetruecolor($w, $h);
         $white = imagecolorallocatealpha($img, 239, 236, 236, 0);
         imagefill($img, 0, 0, $white);
         imagealphablending($img, false);
         imagesavealpha($img, true);
         imagepng($img, $cachefile);
         imagedestroy($img);
     }
     if (file_exists($cachefile)) {
         $url = media_base_url() . 'pixum/' . $hash . $extension;
     } else {
         $url = $this->app->url_manager->site('api_nosession/pixum_img') . "?width=" . $width . "&height=" . $height;
     }
     return $url;
 }
Beispiel #2
0
 function extended_save_images($params)
 {
     if ($this->extended_save_has_permission()) {
         event_trigger('mw.database.extended_save_images', $params);
         $data_to_save = $params;
         if (isset($data_to_save['images'])) {
             $data_fields = $data_to_save['images'];
             if (is_array($data_fields) and !empty($data_fields)) {
                 foreach ($data_fields as $k => $v) {
                     if (isset($v['filename'])) {
                         $save_cat_item = array();
                         $save_cat_item['rel_type'] = $data_to_save['table'];
                         $save_cat_item['rel_id'] = $data_to_save['id'];
                         if (isset($data_to_save['download_remote_images']) and $data_to_save['download_remote_images'] != false) {
                             $is_url = false;
                             if (filter_var($v['filename'], FILTER_VALIDATE_URL)) {
                                 if (!stristr($v['filename'], site_url())) {
                                     $image_src = $v['filename'];
                                     $to_download = false;
                                     $image_src = strtok($image_src, '?');
                                     $ext = get_file_extension($image_src);
                                     switch (strtolower($ext)) {
                                         case 'jpg':
                                         case 'jpeg':
                                         case 'png':
                                         case 'gif':
                                         case 'svg':
                                             $to_download = $image_src;
                                             break;
                                         default:
                                             break;
                                     }
                                     if ($to_download != false) {
                                         $output_fn = 'ext_save' . crc32($to_download) . '.' . $ext;
                                         $relative = 'downloaded' . DS . $save_cat_item['rel_type'] . DS . $save_cat_item['rel_id'] . DS;
                                         $output = media_base_path() . $relative;
                                         $output_relative = media_base_url() . str_replace(DS, '/', $relative);
                                         $output = normalize_path($output, true);
                                         if (!is_dir($output)) {
                                             mkdir_recursive($output);
                                         }
                                         $output_file = $output . $output_fn;
                                         if (!is_file($output_file)) {
                                             $download = new \Microweber\Utils\Http();
                                             $download->set_url($image_src);
                                             $download->download($output_file);
                                         }
                                         $v['filename'] = $output_relative . $output_fn;
                                         $v['filename'] = str_replace(site_url(), '{SITE_URL}', $v['filename']);
                                     }
                                 }
                             }
                         }
                         $save_cat_item["filename"] = $v['filename'];
                         $check = $this->app->media_manager->get($save_cat_item);
                         if ($check == false) {
                             if (isset($v['position'])) {
                                 $save_cat_item["position"] = $v['position'];
                             }
                             $save = $this->app->media_manager->save($save_cat_item);
                         }
                     }
                 }
             }
         }
     }
 }
Beispiel #3
0
 public function thumbnail_img($params)
 {
     extract($params);
     if (!isset($width)) {
         $width = 200;
     }
     if (!isset($height)) {
         $width = 200;
     }
     if (!isset($src) or $src == false) {
         return $this->pixum($width, $height);
     }
     $src = strtok($src, '?');
     $surl = $this->app->url_manager->site();
     $local = false;
     $media_url = media_base_url();
     $media_url = trim($media_url);
     $src = str_replace('{SITE_URL}', $surl, $src);
     $src = str_replace('%7BSITE_URL%7D', $surl, $src);
     $src = str_replace('..', '', $src);
     if (strstr($src, $surl) or strpos($src, $surl)) {
         $src = str_replace($surl . '/', $surl, $src);
         //$src = str_replace($media_url, '', $src);
         $src = str_replace($surl, '', $src);
         $src = ltrim($src, DS);
         $src = ltrim($src, '/');
         $src = rtrim($src, DS);
         $src = rtrim($src, '/');
         //$src = media_base_path() . $src;
         $src = MW_ROOTPATH . $src;
         $src = normalize_path($src, false);
     } else {
         $src1 = media_base_path() . $src;
         $src1 = normalize_path($src1, false);
         $src2 = MW_ROOTPATH . $src;
         $src2 = normalize_path($src2, false);
         if (is_file($src1)) {
             $src = $src1;
         } elseif (is_file($src2)) {
             $src = $src2;
         } else {
             $no_img = true;
             //                if (function_exists('getimagesize')) {
             //                    $im = @getimagesize($src);
             //                    if ($im) {
             //                        if (isset($im['mime'])) {
             //                            $save_ext = false;
             //                            switch ($im['mime']) {
             //                                case "image/jpeg":
             //                                case "image/jpg":
             //                                    $save_ext = 'jpg';
             //                                    break;
             //                                case "image/gif":
             //                                    $save_ext = 'gif';
             //                                    break;
             //                                case "image/png":
             //                                    $save_ext = 'png';
             //                                    break;
             //                                case "image/bmp":
             //                                    $save_ext = 'bmp';
             //                                    break;
             //                            }
             //                            if ($save_ext != false) {
             //                                $ext = $save_ext;
             //                                $no_img = false;
             //                            }
             //                        }
             //
             //                    }
             //                }
             if ($no_img) {
                 return $this->pixum_img();
             }
         }
     }
     $media_root = media_base_path();
     if (!is_writable($media_root)) {
         $media_root = mw_cache_path();
     }
     $cd = $this->thumbnails_path() . $width . DS;
     if (!is_dir($cd)) {
         mkdir_recursive($cd);
     }
     $index_file = $cd . 'index.html';
     if (!is_file($index_file)) {
         file_put_contents($index_file, 'Thumbnail directory is not allowed');
     }
     if (!isset($ext)) {
         $ext = strtolower(get_file_extension($src));
     }
     $cache = md5(serialize($params)) . '.' . $ext;
     $cache = str_replace(' ', '_', $cache);
     if (isset($cache_id)) {
         $cache = str_replace(' ', '_', $cache_id);
         $cache = str_replace('..', '', $cache);
     }
     $cache_path = $cd . $cache;
     if (file_exists($cache_path)) {
         if (!headers_sent()) {
             if (isset($_SERVER['HTTP_IF_MODIFIED_SINCE'])) {
                 $if_modified_since = preg_replace('/;.*$/', '', $_SERVER['HTTP_IF_MODIFIED_SINCE']);
             } else {
                 $if_modified_since = '';
             }
             $mtime = filemtime($src);
             $gmdate_mod = gmdate('D, d M Y H:i:s', $mtime) . ' GMT';
             if ($if_modified_since == $gmdate_mod) {
                 header("HTTP/1.0 304 Not Modified");
             }
         }
     } else {
         if (file_exists($src)) {
             if ($ext == 'svg') {
                 $res1 = file_get_contents($src);
                 $res1 = $this->svgScaleHack($res1, $width, $height);
                 file_put_contents($cache_path, $res1);
             } else {
                 if ($ext == 'jpg' || $ext == 'jpeg' || $ext == 'gif' || $ext == 'png' || $ext == 'bmp') {
                     $tn = new \Microweber\Utils\Thumbnailer($src);
                     $thumbOptions = array('maxLength' => $height, 'width' => $width);
                     $tn->createThumb($thumbOptions, $cache_path);
                     unset($tn);
                 } else {
                     return $this->pixum_img();
                 }
             }
         }
     }
     $ext = get_file_extension($cache_path);
     if ($ext == 'jpg') {
         $ext = 'jpeg';
     }
     header("Content-Type: image/" . $ext);
     header("Content-Length: " . filesize($cache_path));
     readfile($cache_path);
     exit;
 }