Example #1
0
 public static function renewCache($fileOriginal, $fileCached, $presetName)
 {
     //TODO: move all verifications from image.php here
     $presetsList = (include 'image_config.php');
     if (empty($presetsList[$presetName])) {
         throw new RuntimeException('Image preset does not exist!');
     }
     $preset = $presetsList[$presetName];
     if (file_exists($fileCached)) {
         if (time() - filemtime($fileCached) < rad_config::getParam('cache.power.time')) {
             return true;
         }
     } else {
         $cachePath = dirname($fileCached);
         if (!is_dir($cachePath) && !recursive_mkdir($cachePath)) {
             throw new RuntimeException('Could not create cache folder for image');
         }
     }
     $img = new self();
     if (!$img->set($fileOriginal, $fileCached, $preset) || !$img->resize()) {
         throw new RuntimeException('Image conversion error: ' . $img->getError());
     }
 }
 /** 
  * 用一张PNG图片给原始图片加水印,水印图片将自动调整到目标图片大小 
  * 
  * @param string $png png图片的路径 
  * @param string $hp 水平位置 left|center|right 
  * @param string $vp 垂直位置 top|center|bottom 
  * @param int    $pct 水印的透明度 0-100, 0为完全透明,100为完全不透明,只适用于非PNG图片水印 
  * @param string $path 如果指定则生成图片到$path 
  * @param  
  * @return 
  */
 function waterMark($markImg, $hp = 'center', $vp = 'center', $pct = 50, $path = null)
 {
     //原图信息
     $srcw = $this->getWidth();
     $srch = $this->getHeight();
     //水印图信息
     $mark = new self($markImg);
     $markw = $mark->getWidth();
     $markh = $mark->getHeight();
     //水印图片大于目标图片,调整大小
     if ($markw > $srcw || $markh > $srch) {
         //先将水印图片调整到原始图片大小-10个像素
         $mark->resize($srcw - 10, $srch - 10, true);
         $markw = $mark->getWidth();
         $markh = $mark->getHeight();
     }
     //判断水印位置
     $arrx = array('left' => 0, 'center' => floor(($srcw - $markw) / 2), 'right' => $srcw - $markw);
     $arry = array('top' => 0, 'center' => floor(($srch - $markh) / 2), 'bottom' => $srch - $markh);
     $x = isset($arrx[$hp]) ? $arrx[$hp] : $arrx['center'];
     $y = isset($arry[$vp]) ? $arry[$vp] : $arry['center'];
     //png图片水印
     if ($mark->getType() == 3) {
         //打开混色模式
         imagealphablending($this->img, true);
         imagecopy($this->img, $mark->getResource(), $x, $y, 0, 0, $markw, $markh);
     } else {
         imagecopymerge($this->img, $mark->getResource(), $x, $y, 0, 0, $markw, $markh, $pct);
     }
     unset($mark);
     if ($path) {
         return $this->save($path);
     }
     return $this;
 }
Example #3
0
 public static function thumbnail($source, $destination, $maxwidth = 150, $maxheight = 150, $scale = true)
 {
     if (file_exists($destination)) {
         return true;
     }
     $img = new self();
     $img->createfromfile($source);
     $img->resize($maxwidth, $maxheight, $scale);
     $img->interlace();
     $ret = $img->save($destination);
     $img->close();
     return $ret;
 }