function thumbnail($upfiledir, $src, $tName, $tw = '', $th = '', $scale = true, $tDir = "thumb") { global $iCMS; $R = array(); $tw = empty($tw) ? $iCMS->config['thumbwidth'] : (int) $tw; $th = empty($th) ? $iCMS->config['thumbhight'] : (int) $th; if ($tw && $th) { list($width, $height, $type) = @getimagesize($src); if ($width < 1 && $height < 1) { $R['width'] = $tw; $R['height'] = $th; $R['src'] = $src; return $R; } if ($width > $tw || $height > $th) { $R['src'] = $upfiledir . $tDir . "/" . $tName . '_' . $tw . 'x' . $th . '.' . substr(strrchr($src, "."), 1); if (in_array('Gmagick', get_declared_classes())) { $image = new Gmagick(); $image->readImage($src); $im = self::scale(array("tw" => $tw, "th" => $th, "w" => $image->getImageWidth(), "h" => $image->getImageHeight())); $image->resizeImage($im['w'], $im['h'], null, 1); $image->cropImage($tw, $th, 0, 0); //$image->thumbnailImage($gm_w,$gm_h); FS::mkdir($upfiledir . $tDir); $image->writeImage($R['src']); $image->destroy(); } else { $im = self::scale(array("tw" => $tw, "th" => $th, "w" => $width, "h" => $height), $scale); $R['width'] = $im['w']; $R['height'] = $im['h']; $res = self::imagecreate($type, $src); if ($res) { $thumb = imagecreatetruecolor($im['w'], $im['h']); imagecopyresampled($thumb, $res, 0, 0, 0, 0, $im['w'], $im['h'], $width, $height); //PHP_VERSION != '4.3.2' && self::UnsharpMask($thumb); FS::mkdir($upfiledir . $tDir); self::image($thumb, $type, $R['src']); } else { $R['src'] = $src; } } } else { $R['width'] = $width; $R['height'] = $height; $R['src'] = $src; } return $R; } }
/** * Apply transformations on image * * @param string $source Source image * @param array $params Transformations and parameters * @param string $store Temporary store on disk * * @return string */ public function transform($source, $params, $store = null) { try { $image = new \Gmagick(); $image->readImage($source); if (isset($params['negate'])) { $this->canNegate(); } if (isset($params['rotate'])) { $image->rotateImage('#000', $params['rotate']['angle']); } if (isset($params['crop'])) { $image->cropImage($params['crop']['width'], $params['crop']['height'], $params['crop']['x'], $params['crop']['y']); } if (isset($params['contrast'])) { $this->canContrast(); } if (isset($params['brightness'])) { $value = (int) $params['brightness']; $brightness = null; if ($value <= 0) { $brightness = $value + 100; } else { $brightness = $value * 3 + 100; } $image->modulateImage($brightness, 100, 100); } $ret = null; if ($store !== null) { $ret = $image->writeImage($store); } else { $ret = $image->getImageBlob(); } $image->destroy(); return $ret; } catch (\GmagickException $e) { $image->destroy(); throw new \RuntimeException($e->getMessage()); } }
} } } } // Edit upload location here $destination_path = 'files/users/'; $target_path = $destination_path . $name; $Thumb_Path = $destination_path . 'thumbs/' . $name; if (@move_uploaded_file($_FILES['userimg']['tmp_name'], $target_path)) { $result = 1; $sql = execute_sqlQuery("UPDATE tblAppUsers SET userImage='{$name}' WHERE empl_id='{$uid}'"); try { // initialize object $image = new Gmagick(); $image->readImage($target_path); $image->thumbnailImage(55, 55); $image->writeImage($Thumb_Path); // free resource handle $image->destroy(); } catch (Exception $e) { die($e->getMessage()); } } } } } header("Location:edituser.php?res={$result}&er={$er}"); ?>
public static function thumbnail($src, $tw = "0", $th = "0", $scale = true) { if (!self::$config['thumb']['enable']) { return; } $rs = array(); $tw = empty($tw) ? self::$config['thumb']['width'] : (int) $tw; $th = empty($th) ? self::$config['thumb']['height'] : (int) $th; if ($tw && $th) { list($width, $height, $type) = getimagesize($src); if ($width < 1 && $height < 1) { $rs['width'] = $tw; $rs['height'] = $th; $rs['src'] = $src; return $rs; } if ($width > $tw || $height > $th) { $rs['src'] = $src . '_' . $tw . 'x' . $th . '.jpg'; if (in_array('Gmagick', get_declared_classes())) { $image = new Gmagick(); $image->readImage($src); $im = self::scale(array("tw" => $tw, "th" => $th, "w" => $image->getImageWidth(), "h" => $image->getImageHeight())); $image->resizeImage($im['w'], $im['h'], null, 1); $image->cropImage($tw, $th, 0, 0); //$image->thumbnailImage($gm_w,$gm_h); $image->writeImage($rs['src']); $image->destroy(); } else { $im = self::scale(array("tw" => $tw, "th" => $th, "w" => $width, "h" => $height), $scale); $ret = self::imagecreate($type, $src); $rs['width'] = $im['w']; $rs['height'] = $im['h']; if ($ret) { $thumb = imagecreatetruecolor($im['w'], $im['h']); imagecopyresampled($thumb, $ret, 0, 0, 0, 0, $im['w'], $im['h'], $width, $height); self::image($thumb, $type, $rs['src']); } else { $rs['src'] = $src; } } } else { $rs['src'] = $src; $rs['width'] = $width; $rs['height'] = $height; } return $rs; } }
/** * 生成水印 * @param string $groundImage */ function waterImage($groundImage = '') { try { //获取背景图的高,宽 if ($groundImage && is_file($groundImage)) { $bg = new Gmagick(); $bg->readImage($groundImage); $bgHeight = $bg->getImageHeight(); $bgWidth = $bg->getImageWidth(); } //获取水印图的高,宽 if ($this->waterImage && is_file($this->waterImage)) { $water = new Gmagick($this->waterImage); $waterHeight = $water->getImageHeight(); $waterWidth = $water->getImageWidth(); } //如果背景图的高宽小于水印图的高宽则不加水印 if ($bgHeight < $waterHeight || $bgWidth < $waterWidth) { return false; } else { $isWaterImg = TRUE; } //加水印 if ($isWaterImg) { $dw = new GmagickDraw(); //加图片水印 if (is_file($this->waterImage)) { //水印位置随机 $waterPos = $this->getWaterPos($bgWidth, $bgHeight, $waterWidth, $waterHeight); $bg->compositeImage($water, 1, $waterPos['x'], $waterPos['y']); if (!$bg->writeImage($groundImage)) { return FALSE; } } else { //加文字水印 $waterTextInfo = array('textFont' => '15', 'textColor' => '#FF0000', 'textAlpha' => 1, 'textInfo' => 'www.okooo.com'); $dw->setFontSize($waterTextInfo['textFont']); //$dw->setFillColor($waterTextInfo['textColor']); $dw->setFillOpacity(1); $x = abs(130 - $bgWidth); $y = abs(15 - $bgHeight); $dw->annotate($x, $y, $waterTextInfo['textInfo']); $dw->setTextEncoding('UTF-8'); $bg->drawImage($dw); if (!$bg->writeImage($groundImage)) { return FALSE; } } } } catch (Exception $e) { Logger::getLogger('dataengine.lottery.snapshot')->apps('exception')->info(json_encode($e->getMessage())); } }