protected function getImage($image, &$width, &$height) { if (is_object($image) && $image instanceof image_gmagick) { $width = $image->width; $height = $image->height; return $image->image; } elseif (is_object($image) && $image instanceof Gmagick) { try { $w = $image->getImageWidth(); $h = $image->getImageHeight(); } catch (Exception $e) { return false; } $width = $w; $height = $h; return $image; } elseif (is_string($image)) { try { $image = new Gmagick($image); $w = $image->getImageWidth(); $h = $image->getImageHeight(); } catch (Exception $e) { return false; } $width = $w; $height = $h; return $image; } else { return false; } }
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; } }
/** * Retrieve image informations * * @param string $path Image path * * @return array( * 'width', * 'height', * 'type', * 'mime' * ) */ public function getImageInfos($path) { $image = new \Gmagick(); $infos = array(); try { $image->readImage($path); $infos[] = $image->getImageWidth(); $infos[] = $image->getImageHeight(); $infos[] = $image->getImageFormat(); $infos[] = null; //no way to retrieve mimetype } catch (\GmagickException $e) { $image->destroy(); throw new \RuntimeException($e->getMessage()); } $image->destroy(); return $infos; }
//if (in_array($type,$allowedExtensions)) { // Edit upload location here $destination_path = 'files/chat_files/'; $target_path = UPLOAD_PATH . $name; $Thumb_Path = UPLOAD_PATH . 'thumbs/' . $name; if (move_uploaded_file($_FILES['myfile']['tmp_name'], $target_path)) { $result = 1; //$sql = execute_sqlQuery("UPDATE tbl_CompInfo SET comp_Logo='$name' WHERE comp_Id='$comp_Id'"); $MF->updateDiskSpace($size); if (in_array($file_type, array("image/gif", "image/jpeg", "image/png", "image/pjpeg", "image/bmp"))) { try { copy($target_path, $Thumb_Path); // initialize object $image = new Gmagick(); $image->readImage($target_path); $old_width = $image->getImageWidth(); $old_height = $image->getImageHeight(); $new_width = 200; $new_height = $old_height / $old_width * $new_width; exec("gm mogrify -resize {$new_width}x{$new_height} -quality 90 " . APP_INSTALLPATH . UPLOAD_PATH . "thumbs/{$name}"); //$image->thumbnailImage($new_width, $new_height); //$thumbFile = dirname($file) . '/' . basename($target_path, '.JPG') . '.thumb.jpg'; //$image->writeImage($Thumb_Path); /*$thumb_size = $image->getsize(); $MF->updateDiskSpace($thumb_size);*/ // free resource handle $image->destroy(); } catch (Exception $e) { die($e->getMessage()); } }
<?php require_once "TestSettings.php"; dl("gmagick.so"); $input_file = __DIR__ . '/' . TestSettings::INPUT_FILE; $output_file = __DIR__ . "/output/gmagick.jpg"; $timers = ['new' => 0, 'open' => 0, 'get_info' => 0, 'scale' => 0, 'rotate' => 0, 'paste' => 0, 'save' => 0]; for ($i = 0; $i < TestSettings::NUM_RUNS; $i++) { $time_open_start = microtime(true); $Image1 = new Gmagick(); $Image1->readImage($input_file); $timers['open'] += microtime(true) - $time_open_start; $time_get_info_start = microtime(true); $width = $Image1->getImageWidth(); $height = $Image1->getImageHeight(); $timers['get_info'] += microtime(true) - $time_get_info_start; $time_get_info_start = microtime(true); $new_width = ceil($width / TestSettings::DOWNSCALE_FACTOR); $new_height = ceil($height / TestSettings::DOWNSCALE_FACTOR); $timers['get_info'] += microtime(true) - $time_get_info_start; $time_scale_start = microtime(true); $Image1->scaleImage($new_width, $new_height); $timers['scale'] += microtime(true) - $time_scale_start; $time_rotate_start = microtime(true); $WhiteColor = new GmagickPixel('white'); $Image1->rotateImage($WhiteColor, TestSettings::ROTATE_ANGLE); $timers['rotate'] += microtime(true) - $time_rotate_start; $time_new_start = microtime(true); $Image2 = new Gmagick(); $Image2->newImage(TestSettings::OUTPUT_IMAGE_WIDTH, TestSettings::OUTPUT_IMAGE_HEIGHT, 'white'); $timers['new'] += microtime(true) - $time_new_start;
break; case 2: $res = imagecreatefromjpeg($src); break; case 3: $res = imagecreatefrompng($src); break; } return $res; } } } if (fsexists($src)) { $image = new Gmagick(); $image->readImage($src); $scale = array("tw" => $tw, "th" => $th, "w" => $image->getImageWidth(), "h" => $image->getImageHeight()); if ($tw > 0 && $th > 0) { $im = scale($scale); $image->resizeImage($im['w'], $im['h'], null, 1); $x = $y = 0; $im['w'] > $im['tw'] && ($x = ceil(($im['w'] - $im['tw']) / 3)); $im['h'] > $im['th'] && ($y = ceil(($im['h'] - $im['th']) / 3)); $image->cropImage($tw, $th, $x, $y); } else { empty($scale['th']) && ($scale['th'] = 9999999); $im = bitscale($scale); $image->resizeImage($im['w'], $im['h'], null, 1); } $expires = 31536000; header("Cache-Control: maxage=" . $expires); header('Last-Modified: ' . gmdate('D, d M Y H:i:s', $_SERVER['REQUEST_TIME']) . ' GMT');
/** * 生成缩略图 * @param [type] $path [原图路径] * @param integer $tw [缩略图宽度] * @param integer $th [缩略图高度] * @return [image] [缩略图资源] */ public static function make($path, $tw = 1, $th = 1) { strpos($path, '..') === false or exit('What are you doing?'); $srcPath = iPHP_RES_PAHT . $path; $thumbPath = $srcPath . '_' . $tw . 'x' . $th . '.jpg'; if (empty($path) || !self::exists($srcPath)) { return self::blank(); } iPHP_RES_CACHE && (self::$srcData = self::cache($thumbPath, 'get')); if (empty(self::$srcData)) { $gmagick = new Gmagick(); $gmagick->readImage($srcPath); $scale = array("tw" => $tw, "th" => $th, "w" => $gmagick->getImageWidth(), "h" => $gmagick->getImageHeight()); if ($tw > 0 && $th > 0) { $im = self::scale($scale); $gmagick->resizeImage($im['w'], $im['h'], null, 1); $x = $y = 0; $im['w'] > $im['tw'] && ($x = ceil(($im['w'] - $im['tw']) / 3)); $im['h'] > $im['th'] && ($y = ceil(($im['h'] - $im['th']) / 3)); $gmagick->cropImage($tw, $th, $x, $y); } else { empty($scale['th']) && ($scale['th'] = 9999999); $im = self::bitScale($scale); $gmagick->resizeImage($im['w'], $im['h'], null, 1); } header('X-Thumb-Cache: MAKE-' . $_SERVER['REQUEST_TIME']); self::$srcData = $gmagick->current(); iPHP_RES_CACHE && self::cache($thumbPath, self::$srcData); } }
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())); } }