public static function run(\Image $res, $settings) { $resource = $res->Resource(); $dst_w = Image::width($resource); $dst_h = Image::height($resource); $width = $settings['settings']['width']; $height = $settings['settings']['height']; if (!empty($width) && !empty($height)) { $dst_w = $width; $dst_h = $height; } elseif (empty($height)) { $ratio = $dst_h / $dst_w; $dst_w = $width; $dst_h = round($dst_w * $ratio); } elseif (empty($width)) { $ratio = $dst_w / $dst_h; $dst_h = $height; $dst_w = round($dst_h * $ratio); } $tmp = imagecreatetruecolor($dst_w, $dst_h); self::__fill($resource, $tmp, $settings['settings']['background']); imagecopyresampled($tmp, $resource, 0, 0, 0, 0, $dst_w, $dst_h, Image::width($resource), Image::height($resource)); if (is_resource($resource)) { imagedestroy($resource); } $res->setResource($tmp); return $res; }
public static function run($res, $width, $height, $anchor = self::TOP_LEFT, $background_fill = null) { $dst_w = Image::width($res); $dst_h = Image::height($res); if (!empty($width) && !empty($height)) { $dst_w = $width; $dst_h = $height; } else { if (empty($height)) { $ratio = $dst_h / $dst_w; $dst_w = $width; $dst_h = round($dst_w * $ratio); } else { if (empty($width)) { $ratio = $dst_w / $dst_h; $dst_h = $height; $dst_w = round($dst_h * $ratio); } } } $tmp = imagecreatetruecolor($dst_w, $dst_h); self::__fill($res, $tmp, $background_fill); $image_width = Image::width($res); $image_height = Image::height($res); list($src_x, $src_y, $dst_x, $dst_y) = self::__calculateDestSrcXY($dst_w, $dst_h, $image_width, $image_height, $image_width, $image_height, $anchor); imagecopyresampled($tmp, $res, $src_x, $src_y, $dst_x, $dst_y, $image_width, $image_height, $image_width, $image_height); if (is_resource($res)) { imagedestroy($res); } return $tmp; }
public static function run($res, $width = NULL, $height = NULL) { $dst_w = Image::width($res); $dst_h = Image::height($res); if (!empty($width) && !empty($height)) { $dst_w = $width; $dst_h = $height; } elseif (empty($height)) { $ratio = $dst_h / $dst_w; $dst_w = $width; $dst_h = round($dst_w * $ratio); } elseif (empty($width)) { $ratio = $dst_w / $dst_h; $dst_h = $height; $dst_w = round($dst_h * $ratio); } $dst = imagecreatetruecolor($dst_w, $dst_h); /* making the new image transparent */ $background = imagecolorallocate($dst, 0, 0, 0); ImageColorTransparent($dst, $background); // make the new temp image all transparent imagealphablending($dst, false); imagesavealpha($dst, true); imageAntiAlias($dst, true); self::__fill($dst); imagecopyresampled($dst, $res, 0, 0, 0, 0, $dst_w, $dst_h, Image::width($res), Image::height($res)); @imagedestroy($res); return $dst; }
function resize($path, $opt) { $f3 = \Base::instance(); $hash = $f3->hash($path . $f3->serialize($opt)); $new_file_name = $hash . '.jpg'; $dst_path = $f3->get('UPLOADS') . 'img/'; $path = explode('/', $path); $file = array_pop($path); if (!is_dir($dst_path)) { mkdir($dst_path, 0775); } if (!file_exists($dst_path . $new_file_name)) { $imgObj = new \Image($file, false, implode('/', $path) . '/'); $ow = $imgObj->width(); $oh = $imgObj->height(); if (!$opt['width']) { $opt['width'] = round($opt['height'] / $oh * $ow); } if (!$opt['height']) { $opt['height'] = round($opt['width'] / $ow * $oh); } $imgObj->resize((int) $opt['width'], (int) $opt['height'], $opt['crop'], $opt['enlarge']); $file_data = $imgObj->dump('jpeg', null, $opt['quality']); $f3->write($dst_path . $new_file_name, $file_data); } return $dst_path . $new_file_name; }
public function putWatermarkInTiles(Image $wm, $hide = 100) { for ($y = 0; $y < $this->height(); $y += $wm->height()) { for ($x = 0; $x < $this->width(); $x += $wm->width()) { $this->putWatermark($wm, $hide, $x, $y); } } }
public static function run(\Image $res, $settings) { $src_w = $res->Meta()->width; $src_h = $res->Meta()->height; if ($settings['settings']['height'] == 0) { $ratio = $src_h / $src_w; $dst_h = round($settings['meta']['width'] * $ratio); } elseif ($settings['settings']['width'] == 0) { $ratio = $src_w / $src_h; $dst_w = round($settings['meta']['height'] * $ratio); } $src_r = $src_w / $src_h; $dst_r = $settings['meta']['width'] / $settings['meta']['height']; if ($src_r < $dst_r) { $width = $settings['meta']['width']; $height = null; } else { $width = null; $height = $settings['meta']['height']; } $resource = $res->Resource(); $dst_w = Image::width($resource); $dst_h = Image::height($resource); if (!empty($width) && !empty($height)) { $dst_w = $width; $dst_h = $height; } elseif (empty($height)) { $ratio = $dst_h / $dst_w; $dst_w = $width; $dst_h = round($dst_w * $ratio); } elseif (empty($width)) { $ratio = $dst_w / $dst_h; $dst_h = $height; $dst_w = round($dst_h * $ratio); } $image_width = Image::width($resource); $image_height = Image::height($resource); $tmp = imagecreatetruecolor($dst_w, $dst_h); self::__fill($resource, $tmp, $settings['settings']['background']); list($src_x, $src_y, $dst_x, $dst_y) = self::__calculateDestSrcXY($dst_w, $dst_h, $image_width, $image_height, $image_width, $image_height, $settings['settings']['position']); imagecopyresampled($tmp, $resource, $src_x, $src_y, $dst_x, $dst_y, $image_width, $image_height, $image_width, $image_height); if (is_resource($resource)) { imagedestroy($resource); } $res->setResource($tmp); return $res; }
public function saveImage() { $this->allowtypes = array('jpg', 'jpeg', 'png', 'gif'); $this->maxsize = 1024 * 1024 * 5; $filename = $this->setfilename(); $filepath = date('Y') . '/' . date('m') . '/' . $filename; $thumb = 'thumb/' . $filepath; $attachment = 'photo/' . $filepath; if ($this->save(C('ATTACHDIR') . $attachment)) { $image = new Image(C('ATTACHDIR') . $attachment); $image->thumb(210, 210); $image->save(C('ATTACHDIR') . $thumb); return array('name' => $filename, 'width' => $image->width(), 'height' => $image->height(), 'type' => $image->type(), 'filesize' => $this->size(), 'attachment' => $attachment, 'thumb' => $thumb); } else { return false; } }
function saveChanges() { $_POST->setType('filename', 'string'); $_POST->setType('cropimgx', 'numeric'); $_POST->setType('cropimgy', 'numeric'); $_POST->setType('cropimgw', 'numeric'); $_POST->setType('cropimgh', 'numeric'); $_REQUEST->setType('mkcopy', 'string'); if ($_REQUEST['filename']) { if ($_REQUEST['mkcopy']) { if ($_POST['filename'] != $this->basename && $_POST['filename']) { if (!file_exists($this->dirname . '/' . $_POST['filename'])) { $p = $this->dirname . '/' . $_POST['filename']; } else { Flash::queue(__('File exists. Please give another name or delete the conflicting file. Your changes were not saved.'), 'warning'); break; } } else { $nrofcopies = count(glob(substr($this->path, 0, -(strlen($this->extension) + 1)) . '_copy*')); if ($nrofcopies == 0) { $nrofcopies = ''; } $p = substr($this->path, 0, -(strlen($this->extension) + 1)) . '_copy' . ($nrofcopies + 1) . substr($this->path, -(strlen($this->extension) + 1)); } touch($p); $copy = File::open($p); } else { if ($_POST['filename'] != $this->basename) { $this->rename($_POST['filename']); } $p = $this->path; $img = new Image($this->path); if ($_POST['cropimgw'] && $_POST['cropimgh']) { $width = $img->width(); $s = $width / min($width, 400); $img->crop(round($s * $_POST['cropimgx']), round($s * $_POST['cropimgy']), round($s * $_POST['cropimgw']), round($s * $_POST['cropimgh'])); } if ($_REQUEST['imgrot']) { $img->rotate($_REQUEST['imgrot']); } $img->save($p); Flash::queue(__('Your changes were saved')); } } }
function correct($path) { // Уменьшаем до максимально допустимых размеров //var_dump($path); $img = new \Image($path); $img->resize(1500, 1500, false, false); if ($this->fw->get('site.use_watermark')) { $overlay = new \Image('../' . APP_FOLDER . '/views/' . $this->fw->get('site.tpl') . '/watermark.png'); $overlay->resize(1500, 1500, true, false); $img->overlay($overlay, (int) $this->fw->get('site.watermark.x') | (int) $this->fw->get('site.watermark.y')); } elseif ($this->fw->get('site.use_text_watermark')) { // Создаём изображение-оверлей с надписью $wm = $this->createTextWatermark($img->width(), $img->height()); $overlay = new \Image($wm); // 2 добавить его к текущему изборажению $img->overlay($overlay, \Image::POS_Center | \Image::POS_Middle); } $this->fw->write($path, $img->dump()); }
public static function run($res, $width, $height, $anchor = self::TOP_LEFT, $background_fill = 'fff') { $dst_w = Image::width($res); $dst_h = Image::height($res); if (!empty($width) && !empty($height)) { $dst_w = $width; $dst_h = $height; } elseif (empty($height)) { $ratio = $dst_h / $dst_w; $dst_w = $width; $dst_h = round($dst_w * $ratio); } elseif (empty($width)) { $ratio = $dst_w / $dst_h; $dst_h = $height; $dst_w = round($dst_h * $ratio); } $tmp = imagecreatetruecolor($dst_w, $dst_h); self::__fill($tmp, $background_fill); list($src_x, $src_y, $dst_x, $dst_y) = self::__calculateDestSrcXY($dst_w, $dst_h, Image::width($res), Image::height($res), Image::width($res), Image::height($res), $anchor); imagecopyresampled($tmp, $res, $src_x, $src_y, $dst_x, $dst_y, Image::width($res), Image::height($res), Image::width($res), Image::height($res)); @imagedestroy($res); return $tmp; // self::__copy($tmp, $res, true); }
public static function run($res, $width = NULL, $height = NULL) { $dst_w = Image::width($res); $dst_h = Image::height($res); if (!empty($width) && !empty($height)) { $dst_w = $width; $dst_h = $height; } elseif (empty($height)) { $ratio = $dst_h / $dst_w; $dst_w = $width; $dst_h = round($dst_w * $ratio); } elseif (empty($width)) { $ratio = $dst_w / $dst_h; $dst_h = $height; $dst_w = round($dst_h * $ratio); } $dst = imagecreatetruecolor($dst_w, $dst_h); //imagealphablending( $dst, false ); //imagesavealpha( $dst, true ); self::__fill($dst); imagecopyresampled($dst, $res, 0, 0, 0, 0, $dst_w, $dst_h, Image::width($res), Image::height($res)); @imagedestroy($res); return $dst; }
public function image_special($APP) { $FILE = $APP->get('POST.f'); $PREVIEW = $APP->get('POST.p'); $SPECIALE = $APP->get('POST.e'); $HEIGHT = $APP->get('POST.h'); $WIDTH = $APP->get('POST.w'); $PATH_PARTS = pathinfo($APP->get('UPLOADS') . $FILE); $APP->set('FILE_EXTENSION', strtolower($PATH_PARTS['extension'])); $APP->set('FILE_FILENAME', $PATH_PARTS['filename']); $PROCESS_FILTER = ''; if ($APP->get('FILE_EXTENSION') == 'jpg') { $PROCESS_FILTER = 'jpeg'; } else { if ($APP->get('FILE_EXTENSION') == 'gif') { $PROCESS_FILTER = 'gif'; } else { if ($APP->get('FILE_EXTENSION') == 'png') { $PROCESS_FILTER = 'png'; } else { if ($APP->get('FILE_EXTENSION') == 'wbmp') { $PROCESS_FILTER = 'wbmp'; } } } } if ($APP->get('FILE_EXTENSION') == 'png' || $APP->get('FILE_EXTENSION') == 'jpg' || $APP->get('FILE_EXTENSION') == 'jpeg' || $APP->get('FILE_EXTENSION') == 'gif' || $APP->get('FILE_EXTENSION') == 'wbmp') { $img = new Image($FILE, false, $APP->get('UPLOADS')); $IMAGE_WIDTH = 0; $IMAGE_HEIGHT = 0; $IMAGE_WIDTH = $img->width(); $IMAGE_HEIGHT = $img->height(); if ($HEIGHT && $WIDTH && $HEIGHT != $IMAGE_HEIGHT && $WIDTH != $IMAGE_WIDTH) { $img->resize($WIDTH, $HEIGHT, false, false); } switch ($SPECIALE) { case 'invert': $img->invert(); break; case 'grayscale': $img->grayscale(); break; case 'emboss': $img->emboss(); break; case 'sepia': $img->sepia(); break; case 'pixelate': $img->pixelate(10); break; case 'rotate90': $img->rotate(-90); break; case 'rotate180': $img->rotate(-180); break; case 'rotate270': $img->rotate(-270); break; case 'hflip': $img->hflip(); break; case 'vflip': $img->vflip(); break; case 'origin': echo $APP->get('CONSTRUCTR_BASE_URL') . '/UPLOADS/' . $FILE; die; break; } $img->save(); $TS = time(); @file_put_contents($APP->get('UPLOADS') . 'TMP/' . $TS . '.png', $img->dump($PROCESS_FILTER)); echo $APP->get('CONSTRUCTR_BASE_URL') . '/UPLOADS/TMP/' . $TS . '.png'; } }
//Upload de la photo selectionnée if ($_FILES['photo_' . $group['id']]['tmp_name'] != null) { $photos_dir = '../images/groupes/'; $tmp_file = $_FILES['photo_' . $group['id']]['tmp_name']; if (!is_uploaded_file($tmp_file)) { exit("Le fichier uploadé est introuvalbe"); } //Vérification extension $type_file = $_FILES['photo_' . $group['id']]['type']; if (!strstr($type_file, 'jpg') && !strstr($type_file, 'jpeg') && !strstr($type_file, 'bmp') && !strstr($type_file, 'gif') && !strstr($type_file, 'png')) { exit("Le fichier n'est pas une image"); } //copie dans le dossier de destination $name_file = 'photo_' . $group['id'] . '.jpg'; if (preg_match('#[\\x00-\\x1F\\x7F-\\x9F/\\\\]#', $name_file)) { exit("Nom de fichier non valide"); } else { if (!move_uploaded_file($tmp_file, $photos_dir . $name_file)) { exit("Impossible de coier le fichier dans {$photos_dir}"); } } $thumb = new Image($photos_dir . $name_file); $thumb->height(200); $thumb->width(268); $thumb->save(); // => fichier uploadé } mysql_query("UPDATE groups SET\tname='" . $_POST['name_' . $group['id']] . "',\n\t\t\t\t\t\t\t\t\t\t\t\t\t\tnbr='" . $_POST['nbr_' . $group['id']] . "',\n\t\t\t\t\t\t\t\t\t\t\t\t\t\thorraire='" . $_POST['horraire_' . $group['id']] . "',\n\t\t\t\t\t\t\t\t\t\t\t\t\t\tage='" . $_POST['age_' . $group['id']] . "'\n\t\t\t\t\t\t\t\t\t\t\tWHERE id='" . $group['id'] . "'") or die(mysql_error()); header('Location: ../index.php?p=groupes'); } mysql_close($connexion);
define('CARD_DIR', dirname(__FILE__) . '/card'); define('SOURCE_DIR', dirname(__FILE__) . '/source'); define('SPOIL_CARD', '_'); define('WIDTH', 140); define('HEIGHT', 105); $cutRange = [[248, 270], [248, 540], [248, 810], [248, 1080], [1323, 270], [1323, 540], [1323, 810], [1323, 1080]]; $images = ['img51.jpg', 'img54.jpg', 'img57.jpg', 'img60.jpg', 'img63.jpg']; $cardnum = [0, 0, 2, 3, 4, 0, 5, 6, 7, 8, 9, 10, 11, 12, 13, 15, 14, 16, 19, 17, 18, 20, 22, 23, 24, 25, 26, 28, 29, 27, 31, 33, 35, 36, 21, 32, 30, 34, 0, 0]; $count = 0; foreach ($images as $imgname) { foreach ($cutRange as $r) { list($x, $y) = $r; $thumb = new Image(SOURCE_DIR . '/' . $imgname); $thumb->width(WIDTH); $thumb->height(HEIGHT); $thumb->crop($x, $y); $thumb->dir(CARD_DIR); $name = $cardnum[$count++]; $thumb->name($name == 0 ? SPOIL_CARD : $name); $thumb->save(); } } // 消費財 $thumb = new Image(SOURCE_DIR . '/' . 'img63.jpg'); $thumb->width(WIDTH); $thumb->height(HEIGHT); $thumb->crop(1323, 1110); $thumb->dir(CARD_DIR); $thumb->name('99'); $thumb->save(); unlink(CARD_DIR . '/' . SPOIL_CARD . '.jpg');
/** * Creates a thumbnail of the image * @param string $file name of the file * @return string */ function getThumb($file) { $image = new Image($this->getPathLocal($file)); if (!in_array(strtolower(pathinfo($file, PATHINFO_EXTENSION)), array('png', 'gif', 'jpg', 'jpeg'))) { $file .= '.jpg'; } /* disabled because of problems with the safe emailer preg_replace queries if($image->thumbnail() !== false) { return $image->thumbnail($this->thumbMaxSize,$this->thumbMaxSize); } else {*/ if (!file_exists($this->getThumbPathLocal($file))) { $w = $image->width(); $h = $image->height(); if ($w >= $h) { $image->resize($this->thumbMaxSize); } else { $image->resize(false, $this->thumbMaxSize); } if (!is_dir($this->getThumbPathLocal(''))) { mkdir($this->getThumbPathLocal(''), 0755, true); } $image->save($this->getThumbPathLocal($file)); } $fname = htmlentities(utf8($file)); return '<img src="' . $this->galleryDirPublic . str_replace('%2F', '/', rawurlencode($this->thumbDirPublic . $this->ualbumName . ($this->ualbumName ? '/' : '') . $fname)) . '" alt="' . $fname . '" />'; //} }
<?php include './Image.php'; echo '<pre>'; $img = '/tmp/20151005/5613e8ca1832d87cb24a35a89b178fca.jpg'; $image = new Image(Image::IMAGE_GD); //GD库处理 $image->open($img); /******************************获取图像信息**********************************************************************************************/ $width = $image->width(); // 返回图片的宽度 $height = $image->height(); // 返回图片的高度 $type = $image->type(); // 返回图片的类型 $mime = $image->mime(); // 返回图片的mime类型 $size = $image->size(); // 返回图片的尺寸数组 0 图片宽度 1 图片高度 /******************************生成缩略图,按照原图的比例生成一个最大为150*150的缩略图并保存为thumb.jpg******************************************/ //$image->thumb(150, 150)->save('/tmp/20151005/thumb.jpg');//我们看到实际生成的缩略图并不是150*150,因为默认采用原图等比例缩放的方式生成缩略图,最大宽度是150。 /* IMAGE_THUMB_SCALE = 1 ; //等比例缩放类型(默认) IMAGE_THUMB_FILLED = 2 ; //缩放后填充类型 IMAGE_THUMB_CENTER = 3 ; //居中裁剪类型 IMAGE_THUMB_NORTHWEST = 4 ; //左上角裁剪类型 IMAGE_THUMB_SOUTHEAST = 5 ; //右下角裁剪类型 IMAGE_THUMB_FIXED = 6 ; //固定尺寸缩放类型 */ /******************************添加图片水印*************************************************************************************************/ /* $image->crop(440, 440)->save('/tmp/20151005/crop.jpg');//将图片裁剪为440x440并保存为corp.jpg $image->water('/tmp/20151005/logo.png')->save("/tmp/20151005/water.gif");// 给裁剪后的图片添加图片水印(水印文件位于./logo.png),位置为右下角,保存为water.gif
/** * @param null $files * @throw \Exception * @return array */ public function upload($files = NULL) { if (empty($files)) { $files = $_FILES; } if (empty($files)) { $this->throwMsg(-2); } if (!$this->uploader->checkRootPath($this->root_path)) { $this->throwMsg(1); } if (!$this->uploader->checkSavePath($this->save_path)) { $this->throwMsg(1); } /* 逐个检测并上传文件 */ $info = array(); if (function_exists('finfo_open')) { $finfo = finfo_open(FILEINFO_MIME_TYPE); } // 对上传文件数组信息处理 $files = $this->dealFiles($files); foreach ($files as $key => $file) { if (!isset($file['key'])) { $file['key'] = $key; } /* 通过扩展获取文件类型,可解决FLASH上传$FILES数组返回文件类型错误的问题 */ if (isset($finfo)) { $file['type'] = @finfo_file($finfo, $file['tmp_name']); } /* 获取上传文件后缀,允许上传无后缀文件 */ $file['ext'] = pathinfo($file['name'], PATHINFO_EXTENSION); /* 文件上传检测 */ if (!$this->check($file)) { continue; } /* 获取文件hash */ if ($this->make_hash) { $file['md5'] = md5_file($file['tmp_name']); $file['sha1'] = sha1_file($file['tmp_name']); } /* 生成保存文件名 */ $save_name = $this->getSaveName($file); if (false == $save_name) { continue; } else { $file['save_name'] = $save_name; } /* 检测并创建子目录 */ $sub_path = $this->get_sub_path($file); if (false === $sub_path) { continue; } else { $file['save_path'] = $this->savePath . $sub_path; } /* 对图像文件进行严格检测 */ $ext = strtolower($file['ext']); if (in_array($ext, array('gif', 'jpg', 'jpeg', 'bmp', 'png', 'swf'))) { $imginfo = getimagesize($file['tmp_name']); if (empty($imginfo) || $ext == 'gif' && empty($imginfo['bits'])) { $this->error = _('Illegal image file!'); continue; } if ($this->image_info) { $file['image'] = []; c_lib()->load('image'); $image = new Image(Image::IMAGE_GD, $file['tmp_name']); $file['image']['width'] = $image->width(); $file['image']['height'] = $image->height(); unset($image); } } /* 保存文件 并记录保存成功的文件 */ if ($this->uploader->save($file, $this->replace)) { unset($file['error'], $file['tmp_name']); $info[$key] = $file; } else { $this->error = $this->uploader->getError(); } } if (empty($info)) { $this->throwMsg(2); } return $info; }