public function thumb($uniqueId, $width, $height, $method, $filename) { // only image can get thumbnails, existing files and valid resize method name if (!$this->MediaFile->isImage() || !($ImageFile = $this->MediaFile->file()) || !method_exists($ImageFile, $method)) { return false; } $filename = STATIC_DIR . 'img/public/' . $uniqueId . '/' . $width . 'x' . $height . '/' . $method . '/' . $filename; $width = (int) $width > 0 ? (int) $width : null; $height = (int) $height > 0 ? (int) $height : null; // resize Image try { $ImageFile->{$method}($width, $height, true, false); // apply sharpen filter if available and image is small if (function_exists('imageconvolution') && $width < 400 && $height < 400) { Library::load('ephFrame.lib.file.image.ImageSharpenFilter'); $sharpenFilter = new ImageSharpenFilter(); $ImageFile->applyFilter($sharpenFilter); } } catch (ImageToLargeToLoadException $e) { $ImageFile = new Image($width, $height); $ImageFile->backgroundColor('ffffe0')->border('e6db55', 1, 0); $ImageFile->text(Image::CENTERED, Image::CENTERED, 'image to large to' . LF . 'create thumbnail', 'b8ad4c', 1); } $ImageFile->saveAs($filename, $this->thumbQuality); $this->redirect(WEBROOT . $filename); }
public function addTextToImage($text, $x, $y, $size, $color, $filename, $newfilename) { if (!file_exists(DIR_IMAGE . $filename) || !is_file(DIR_IMAGE . $filename)) { return; } $info = pathinfo($filename); $extension = $info['extension']; $old_image = $filename; $new_image = $newfilename; $path = ''; $directories = explode('/', dirname(str_replace('../', '', $new_image))); foreach ($directories as $directory) { $path = $path . '/' . $directory; if (!file_exists(DIR_IMAGE . $path)) { @mkdir(DIR_IMAGE . $path, 0777); } } $image = new Image(DIR_IMAGE . $old_image); $image->text($text, $x, $y, $size, $color); $image->save(DIR_IMAGE . $new_image); return true; }
function design($images, $border = '', $effect = '', $saveDir = '', $copyLast = false, $output = 'data') { $id = isset($images[0]['design']) && $images[0]['design'] ? $images[0]['design'] : mt_rand(); $color = strlen($images[0]['color']) == 7 ? substr($images[0]['color'], 1) : $images[0]['color']; $new_image = ($saveDir ? $saveDir : 'cache/') . 'Design-' . $id . '.png'; $path = ''; $directories = explode('/', dirname(str_replace('../', '', $new_image))); foreach ($directories as $directory) { $path = $path . '/' . $directory; if (!file_exists(DIR_IMAGE . $path)) { @mkdir(DIR_IMAGE . $path, 0777); } } $image = new Image(DIR_IMAGE . $images[0]['src']); $width = $images[0]['width']; $height = $images[0]['height']; $image->resize($width, $height, '', $color); if ($images[0]['rotation']) { $image->rotate($images[0]['rotation'] * -1); $rotate = $images[0]['rotation']; } // Look for image Mask, to trim stuff outside the target area. $imagesrc = substr($images[0]['src'], 0, -4) . '-' . $images[0]['rotation'] . substr($images[0]['src'], -4); if (file_exists(DIR_IMAGE . $imagesrc)) { $images[0]['width'] = $image->getInfo('width'); $images[0]['height'] = $image->getInfo('height'); $images[0]['mask'] = 1; $images[0]['src'] = $imagesrc; $images[0]['rotation'] = 0; } if ($copyLast || isset($images[0]['mask'])) { // Cover the top of the image with the base image $images[0]['left'] = $images[0]['top'] = 0; $images[] = $images[0]; } unset($images[0]); foreach ($images as $i => $img) { $image_temp = new Image(DIR_IMAGE . $img['src']); $size = isset($img['size']) && $img['size'] ? $img['size'] / 100 : 1; if ($img['type'] != 'text') { if (isset($images[$i]['crop']) && strpos($images[$i]['crop'], '/')) { $crop = explode('/', $images[$i]['crop']); $ratio = isset($crop[4]) ? $image_temp->getInfo('width') > $image_temp->getInfo('height') ? $image_temp->getInfo('width') / $crop[4] : $image_temp->getInfo('height') / $crop[4] : 1; $image_temp->crop($crop[0] * $ratio, $crop[1] * $ratio, $crop[2] * $ratio, $crop[3] * $ratio); } $image_temp->resize($img['width'] * $size, $img['height'] * $size, isset($img['mask']) ? '' : $effect); if ($img['rotation']) { $image_temp->rotate($img['rotation'] * -1); } } else { $image_temp->text($img['src'], $img['font'], DESIGN_TEXT_DEFAULT_SIZE * $size, DESIGN_FONT_COLOR, '', $img['rotation'] * -1); } $imgLeft = isset($img['left']) ? (int) $img['left'] : 0; $imgTop = isset($img['top']) ? (int) $img['top'] : 0; $opacity = $img['type'] != 'mainProduct' ? 80 : 100; $image->merge($image_temp->getResource(), $imgLeft, $imgTop, $opacity); } /*if ($rotate) { $image->rotate($rotate); $cx = $image->getInfo('width')/2; $cy = $image->getInfo('height')/2; $image->crop($cx - ($width/2),$cy - ($height/2), $cx + ($width/2),$cy + ($height/2)); $image->rotate($rotate*-1);*/ $width = $image->getInfo('width'); $height = $image->getInfo('height'); //} $image->resize($width, $height, $border, $color); if ($output == 'data') { return $image->getData(DIR_IMAGE . $new_image); } else { $image->save(DIR_IMAGE . $new_image); if ($this->request->server['HTTPS']) { return $this->config->get('config_ssl') . 'image/' . $new_image; } else { return $this->config->get('config_url') . 'image/' . $new_image; } } }
* array( * "state" => "", //上传状态,上传成功时必须返回"SUCCESS" * "url" => "", //返回的地址 * "title" => "", //新文件名 * "original" => "", //原始文件名 * "type" => "" //文件类型 * "size" => "", //文件大小 * ) */ $config = (include $rootpath . '/App/Admin/Conf/Water.php'); if ($config['WATER_FUNTION'] == 1) { $info = $up->getFileInfo(); $filePath = $rootpath . $info['url']; $imgObj = new Image(); $imgObj->open($filePath); $waterpos = $config['WATER_POS']; $watercom = $config['WATER_COMPRESSION']; switch ($config['WATER_FILE_TYPE']) { case 1: $waterimg = $rootpath . $config['WATER_IMAGE']; $imgObj->water($waterimg, $waterpos, $watercom); break; case 2: $waterfont = $rootpath . $config['WATER_FONTFILE']; $imgObj->text($config['WATER_TEXT'], $waterfont, $config['WATER_FONTSIZE'], $config['WATER_FONTCOLOR'], $waterpos, 0, $config['WATER_ANGLE']); break; } $imgObj->save($filePath); } /* 返回数据 */ return json_encode($up->getFileInfo());
/** * Print text * * @param \Zork\Image\Image $image * @param string $text * @param int $left * @param int $top * @return \Zork\Image\Font * @codeCoverageIgnore */ public function text(Image $image, $text, $left, $top, $color) { $image->text($text, $left, $top, $color, $this); return $this; }