function imageResize($file, $info, $destination) { $height = $info[1]; //высота $width = $info[0]; //ширина //определяем размеры будущего превью $y = 150; if ($width > $height) { $x = $y * ($width / $height); } else { $x = $y / ($height / $width); } $to = imageCreateTrueColor($x, $y); switch ($info['mime']) { case 'image/jpeg': $from = imageCreateFromJpeg($file); break; case 'image/png': $from = imageCreateFromPng($file); break; case 'image/gif': $from = imageCreateFromGif($file); break; default: echo "No prevue"; break; } imageCopyResampled($to, $from, 0, 0, 0, 0, imageSX($to), imageSY($to), imageSX($from), imageSY($from)); imagepng($to, $destination); imagedestroy($from); imagedestroy($to); }
function image_createThumb($src, $dest, $maxWidth, $maxHeight, $quality = 75) { if (file_exists($src) && isset($dest)) { // path info $destInfo = pathInfo($dest); // image src size $srcSize = getImageSize($src); // image dest size $destSize[0] = width, $destSize[1] = height $srcRatio = $srcSize[0] / $srcSize[1]; // width/height ratio $destRatio = $maxWidth / $maxHeight; if ($destRatio > $srcRatio) { $destSize[1] = $maxHeight; $destSize[0] = $maxHeight * $srcRatio; } else { $destSize[0] = $maxWidth; $destSize[1] = $maxWidth / $srcRatio; } // path rectification if ($destInfo['extension'] == "gif") { $dest = substr_replace($dest, 'jpg', -3); } // true color image, with anti-aliasing $destImage = imageCreateTrueColor($destSize[0], $destSize[1]); // imageAntiAlias($destImage,true); // src image switch ($srcSize[2]) { case 1: //GIF $srcImage = imageCreateFromGif($src); break; case 2: //JPEG $srcImage = imageCreateFromJpeg($src); break; case 3: //PNG $srcImage = imageCreateFromPng($src); break; default: return false; break; } // resampling imageCopyResampled($destImage, $srcImage, 0, 0, 0, 0, $destSize[0], $destSize[1], $srcSize[0], $srcSize[1]); // generating image switch ($srcSize[2]) { case 1: case 2: imageJpeg($destImage, $dest, $quality); break; case 3: imagePng($destImage, $dest); break; } return true; } else { return 'No such File'; } }
protected function get_any_type($srcpath) { try { $this->check_file($srcpath); $srcSize = getImageSize($srcpath); switch ($srcSize[2]) { case 1: $img = imageCreateFromGif($srcpath); break; case 2: $img = imageCreateFromJpeg($srcpath); break; case 3: $img = imageCreateFromPng($srcpath); break; default: throw new Imgdexception('not possible to get any type - srcpath:' . $srcpath); break; } $image['width'] = $srcSize[0]; $image['height'] = $srcSize[1]; $image['img'] = $img; return $image; } catch (Imgdexception $e) { $e->print_debug(__FILE__, __LINE__); return false; } }
function load() { if ($this->loaded) { return true; } //$size = $this->get_size(); //if (!$size) throw new exception("Failed loading image"); list($this->w, $this->h, $this->type) = getImageSize($this->src); switch ($this->type) { case 1: $this->img = imageCreateFromGif($this->src); break; case 2: $this->img = imageCreateFromJpeg($this->src); break; case 3: $this->img = imageCreateFromPng($this->src); break; default: throw new exception("Unsuported image type"); break; } $this->loaded = true; return true; }
/** * Using GD lib for get and transform images. * @see http://php.net/manual/en/function.imagecreatefromjpeg.php#110547 */ function anyImgFile2Im($fname = NULL) { if ($fname) { $this->setFile($fname); } $allowedTypes = [1, 2, 3, 6]; // gif,jpg,png,bmp if (!in_array($this->ftype, $allowedTypes)) { return false; } switch ($this->ftype) { case 1: $im = imageCreateFromGif($this->fname); break; case 2: $im = imageCreateFromJpeg($this->fname); break; case 3: $im = imageCreateFromPng($this->fname); // echo "\n degug {$this->fname}"; break; case 6: $im = imageCreateFromBmp($this->fname); break; } return $im; }
/** * Load image from $fileName * * @throws Zend_Image_Driver_Exception * @param string $fileName Path to image */ public function load($fileName) { parent::load($fileName); $this->_imageLoaded = false; $info = getimagesize($fileName); switch ($this->_type) { case 'jpg': $this->_image = imageCreateFromJpeg($fileName); if ($this->_image !== false) { $this->_imageLoaded = true; } break; case 'png': $this->_image = imageCreateFromPng($fileName); if ($this->_image !== false) { $this->_imageLoaded = true; } break; case 'gif': $this->_image = imageCreateFromGif($fileName); if ($this->_image !== false) { $this->_imageLoaded = true; } break; } }
/** * Generate a thumbnail from an image * */ function make_thumb($source, $destination, $size) { // Check if GD is installed if (extension_loaded('gd') && function_exists('imageCreateFromJpeg')) { // First figure out the size of the thumbnail list($original_x, $original_y) = getimagesize($source); if ($original_x > $original_y) { $thumb_w = $size; $thumb_h = $original_y * ($size / $original_x); } if ($original_x < $original_y) { $thumb_w = $original_x * ($size / $original_y); $thumb_h = $size; } if ($original_x == $original_y) { $thumb_w = $size; $thumb_h = $size; } // Now make the thumbnail $source = imageCreateFromJpeg($source); $dst_img = ImageCreateTrueColor($thumb_w, $thumb_h); imagecopyresampled($dst_img, $source, 0, 0, 0, 0, $thumb_w, $thumb_h, $original_x, $original_y); imagejpeg($dst_img, $destination); // Clear memory imagedestroy($dst_img); imagedestroy($source); // Return true return true; } else { return false; } }
function imageCreateFromAny($filepath) { list($w, $h, $t, $attr) = getimagesize($filepath); // [] if you don't have exif you could use getImageSize() $allowedTypes = array(IMG_GIF, IMG_JPG, IMG_PNG, IMG_WBMP); if (!in_array($t, $allowedTypes)) { return false; } switch ($t) { case IMG_GIF: $im = imageCreateFromGif($filepath); echo 'type image GIF'; break; case IMG_JPG: $im = imageCreateFromJpeg($filepath); echo 'type image JPG'; break; case IMG_PNG: $im = imageCreateFromPng($filepath); echo 'type image PNG'; break; case IMG_WBMP: $im = imageCreateFromwBmp($filepath); echo 'type image BMP'; break; } echo 'source : ' . $filepath . ' | im : ' . $im; return $im; }
function imagecreator($filename) { $nx = 16; $ny = 8; $zoom = 4; $im = imageCreateFromJpeg("uploads/" . $filename); $temp_dir = "dir_{$filename}"; mkdir("uploads/" . $temp_dir, 0744); $temp_dir = "uploads/" . $temp_dir; //return; $imSX = imageSX($im); $imSY = imageSY($im); $kusokX = $imSX / $nx; $kusokY = $imSY / $ny; for ($k = 0; $k < $ny; $k++) { for ($i = 0; $i < $nx; $i++) { $im2 = imagecreatetruecolor(512, 512); imagecopyresized($im2, $im, 0, 0, $i * $kusokX, $k * $kusokY, 512, 512, $kusokX, $kusokY); //imageInterlace($im2, 1); imagejpeg($im2, "{$temp_dir}/" . "1_{$zoom}_{$i}_{$k}_.jpeg", 90); $im2 = null; } } $factor = 2; $zoom = $zoom - 1; for ($k = 0; $k < $ny / $factor; $k++) { for ($i = 0; $i < $nx / $factor; $i++) { $im2 = imagecreatetruecolor(512, 512); imagecopyresized($im2, $im, 0, 0, $i * $kusokX * $factor, $k * $kusokY * $factor, 512, 512, $kusokX * $factor, $kusokY * $factor); //imageInterlace($im2, 1); imagejpeg($im2, "{$temp_dir}/" . "1_{$zoom}_{$i}_{$k}_.jpeg", 90); $im2 = null; } } $factor = 4; $zoom = $zoom - 1; for ($k = 0; $k < $ny / $factor; $k++) { for ($i = 0; $i < $nx / $factor; $i++) { $im2 = imagecreatetruecolor(512, 512); imagecopyresized($im2, $im, 0, 0, $i * $kusokX * $factor, $k * $kusokY * $factor, 512, 512, $kusokX * $factor, $kusokY * $factor); //imageInterlace($im2, 1); imagejpeg($im2, "{$temp_dir}/" . "1_{$zoom}_{$i}_{$k}_.jpeg", 90); $im2 = null; } } $factor = 8; $zoom = $zoom - 1; for ($k = 0; $k < $ny / $factor; $k++) { for ($i = 0; $i < $nx / $factor; $i++) { $im2 = imagecreatetruecolor(512, 512); imagecopyresized($im2, $im, 0, 0, $i * $kusokX * $factor, $k * $kusokY * $factor, 512, 512, $kusokX * $factor, $kusokY * $factor); //imageInterlace($im2, 1); imagejpeg($im2, "{$temp_dir}/" . "1_{$zoom}_{$i}_{$k}_.jpeg", 90); $im2 = null; } } $im2 = imagecreatetruecolor(512, 512); imagecopyresized($im2, $im, 0, 0, 0, 0, 512, 256, $imSX, $imSY); imagejpeg($im2, "{$temp_dir}/" . "1_0_0_0_.jpeg", 90); }
/** * Отримати кольора * @return array */ public function getColors() { $image = imageCreateFromJpeg($this->address); $color = imagecolorat($image, 10, 15); $colors = imagecolorsforindex($image, $color); array_pop($colors); return $colors; }
/** * Read a jpg image from file * @param File file of the image * @return resource internal PHP image resource of the file */ public function read(File $file) { $this->checkIfReadIsPossible($file, $this->supportedExtensions); $image = imageCreateFromJpeg($file->getAbsolutePath()); if ($image === false) { throw new ImageException($file->getPath() . ' is not a valid JPG image'); } return $image; }
public function __construct($uploaded_img_name, $save_path, $name) { $this->_save_path = $save_path; if (!is_dir($this->_save_path)) { mkdir($this->_save_path, 0777); } $this->_new_img_name = $name . '.jpg'; $this->_orig_img = imageCreateFromJpeg($uploaded_img_name); $this->w = imagesx($this->_orig_img); $this->h = imagesy($this->_orig_img); }
/** * @see \wcf\system\image\adapter\IImageAdapter::loadFile() */ public function loadFile($file) { list($this->width, $this->height, $this->type) = getImageSize($file); switch ($this->type) { case IMAGETYPE_GIF: $this->image = imageCreateFromGif($file); break; case IMAGETYPE_JPEG: $this->image = imageCreateFromJpeg($file); break; case IMAGETYPE_PNG: $this->image = imageCreateFromPng($file); break; default: throw new SystemException("Could not read image '" . $file . "', format is not recognized."); break; } }
function loadImage($path) { if ($this->image) { imagedestroy($this->image); } $img_sz = getimagesize($path); switch ($img_sz[2]) { case 1: $this->image_type = "GIF"; if (!($this->image = imageCreateFromGif($path))) { return FALSE; } else { return TRUE; } break; case 2: $this->image_type = "JPG"; if (!($this->image = imageCreateFromJpeg($path))) { return FALSE; } else { return TRUE; } break; case 3: $this->image_type = "PNG"; if (!($this->image = imageCreateFromPng($path))) { return FALSE; } else { return TRUE; } break; case 4: $this->image_type = "SWF"; if (!($this->image = imageCreateFromSwf($path))) { return FALSE; } else { return TRUE; } break; default: return FALSE; } }
public function actionTest() { $i = 10; $g = 20; $photo = new GeneratorImageColorPalette(); // $t1 = $photo->getImageColor('https://scontent.cdninstagram.com/hphotos-xfa1/t51.2885-15/s150x150/e35/11820536_519767031505386_1692984693_n.jpg',$i,$g); // $t2 = $photo->getImageColor('https://scontent.cdninstagram.com/hphotos-xaf1/t51.2885-15/s150x150/e35/11850151_147906375552698_1188110438_n.jpg',$i,$g); // $t3 = $photo->getImageColor('https://scontent.cdninstagram.com/hphotos-xaf1/t51.2885-15/s150x150/e35/11850151_147906375552698_1188110438_n.jpg',$i,$g); // $t4 = $photo->getImageColor('https://scontent.cdninstagram.com/hphotos-xfp1/t51.2885-15/s150x150/e35/1389277_898236996891339_792704999_n.jpg',$i,$g); // $t5 = $photo->getImageColor('https://scontent.cdninstagram.com/hphotos-xfa1/t51.2885-15/s150x150/e35/11375282_1475416576086732_1622940988_n.jpg',$i,$g); $t = 'https://scontent.cdninstagram.com/hphotos-xfa1/t51.2885-15/s150x150/e35/11375282_1475416576086732_1622940988_n.jpg'; // $t = 'https://scontent.cdninstagram.com/hphotos-xfa1/t51.2885-15/s150x150/e35/11820536_519767031505386_1692984693_n.jpg'; // $t = 'https://scontent.cdninstagram.com/hphotos-xaf1/t51.2885-15/s150x150/e35/11850151_147906375552698_1188110438_n.jpg'; $t = 'https://scontent.cdninstagram.com/hphotos-xaf1/t51.2885-15/s150x150/e15/11325634_802930826488058_1599582105_n.jpg'; $t = 'https://scontent.cdninstagram.com/hphotos-xaf1/t51.2885-15/s150x150/e15/11382549_922947047728394_1720425873_n.jpg'; $t = 'https://scontent.cdninstagram.com/hphotos-xaf1/t51.2885-15/s150x150/e15/11287495_1640746426170859_1360888926_n.jpg'; $t = 'https://scontent.cdninstagram.com/hphotos-xaf1/t51.2885-15/s150x150/e15/11358192_686709151434224_172548493_n.jpg'; // $t = 'https://scontent.cdninstagram.com/hphotos-xfa1/t51.2885-15/s150x150/e35/11820536_519767031505386_1692984693_n.jpg'; // $t = 'https://scontent.cdninstagram.com/hphotos-xaf1/t51.2885-15/s150x150/e35/c216.0.648.648/11950711_476949739149891_1670913693_n.jpg'; //Загрузка JPG-изображения из файла Image.jpg $image = imageCreateFromJpeg($t); //Возвращаем цвет пикселя с координатами (10, 15) на изображении $image $color = imagecolorat($image, 10, 15); $colors = imagecolorsforindex($image, $color); //Получаем составляющие цвета (red, green, blue) // $r = ($color >> 16) & 0xFF; // $g = ($color >> 8) & 0xFF; // $b = $color & 0xFF; //Выводим результат // echo $r."<br />"; // echo $g."<br />"; // echo $b."<br />"; // var_dump($colors); var_dump(max(array(5 => 88, 99 => 55))); //Освобождаем ресурсы сервера // imageDestroy($image); $t = new Color($t); echo $t->getDominantColor(); }
function __construct($filename) { $this->_filename = $filename; list($this->_width, $this->_height) = getimagesize($this->_filename); $pathinfo = pathinfo($filename); switch (strtolower($pathinfo['extension'])) { case 'jpg': case 'jpeg': $this->_source = imageCreateFromJpeg($filename); break; case 'gif': $this->_source = imageCreateFromGif($filename); $this->_createImageCallback = 'imagecreate'; break; case 'png': $this->_source = imageCreateFromPng($filename); break; default: throw new Naf_Image_Exception('Unsupported file extension'); break; } }
/** * Generate images of alternate sizes. */ function resizeImage($cnvrt_arry) { global $platform, $imgs, $cnvrt_path, $reqd_image, $convert_writable, $convert_magick, $convert_GD, $convert_cmd, $cnvrt_alt, $cnvrt_mesgs, $compat_quote; if (empty($imgs) || $convert_writable == FALSE) { return; } if ($convert_GD == TRUE && !($gd_version = gdVersion())) { return; } if ($cnvrt_alt['no_prof'] == TRUE) { $strip_prof = ' +profile "*"'; } else { $strip_prof = ''; } if ($cnvrt_alt['mesg_on'] == TRUE) { $str = ''; } foreach ($imgs as $img_file) { if ($cnvrt_alt['indiv'] == TRUE && $img_file != $reqd_image['file']) { continue; } $orig_img = $reqd_image['pwd'] . '/' . $img_file; $cnvrtd_img = $cnvrt_path . '/' . $cnvrt_arry['prefix'] . $img_file; if (!is_file($cnvrtd_img)) { $img_size = GetImageSize($orig_img); $height = $img_size[1]; $width = $img_size[0]; $area = $height * $width; $maxarea = $cnvrt_arry['maxwid'] * $cnvrt_arry['maxwid'] * 0.9; $maxheight = $cnvrt_arry['maxwid'] * 0.75 + 1; if ($area > $maxarea || $width > $cnvrt_arry['maxwid'] || $height > $maxheight) { if ($width / $cnvrt_arry['maxwid'] >= $height / $maxheight) { $dim = 'W'; } if ($height / $maxheight >= $width / $cnvrt_arry['maxwid']) { $dim = 'H'; } if ($dim == 'W') { $cnvt_percent = round(0.9375 * $cnvrt_arry['maxwid'] / $width * 100, 2); } if ($dim == 'H') { $cnvt_percent = round(0.75 * $cnvrt_arry['maxwid'] / $height * 100, 2); } // convert it if ($convert_magick == TRUE) { // Image Magick image conversion if ($platform == 'Win32' && $compat_quote == TRUE) { $winquote = '"'; } else { $winquote = ''; } exec($winquote . $convert_cmd . ' -geometry ' . $cnvt_percent . '%' . ' -quality ' . $cnvrt_arry['qual'] . ' -sharpen ' . $cnvrt_arry['sharpen'] . $strip_prof . ' "' . $orig_img . '"' . ' "' . $cnvrtd_img . '"' . $winquote); $using = $cnvrt_mesgs['using IM']; } elseif ($convert_GD == TRUE) { // GD image conversion if (eregi('\\.jpg$|\\.jpeg$', $img_file) == TRUE && (imageTypes() & IMG_JPG) == TRUE) { $src_img = imageCreateFromJpeg($orig_img); } elseif (eregi('\\.gif$', $img_file) == TRUE && (imageTypes() & IMG_PNG) == TRUE) { $src_img = imageCreateFromPng($orig_img); } elseif (eregi('\\.gif$', $img_file) == TRUE && (imageTypes() & IMG_GIF) == TRUE) { $src_img = imageCreateFromGif($orig_img); } else { continue; } $src_width = imageSx($src_img); $src_height = imageSy($src_img); $dest_width = $src_width * ($cnvt_percent / 100); $dest_height = $src_height * ($cnvt_percent / 100); if ($gd_version >= 2) { $dst_img = imageCreateTruecolor($dest_width, $dest_height); imageCopyResampled($dst_img, $src_img, 0, 0, 0, 0, $dest_width, $dest_height, $src_width, $src_height); } else { $dst_img = imageCreate($dest_width, $dest_height); imageCopyResized($dst_img, $src_img, 0, 0, 0, 0, $dest_width, $dest_height, $src_width, $src_height); } imageDestroy($src_img); if (eregi('\\.jpg$|\\.jpeg$/i', $img_file) == TRUE && (imageTypes() & IMG_JPG) == TRUE) { imageJpeg($dst_img, $cnvrtd_img, $cnvrt_arry['qual']); } elseif (eregi('\\.gif$', $img_file) == TRUE && (imageTypes() & IMG_PNG) == TRUE) { imagePng($dst_img, $cnvrtd_img); } elseif (eregi('\\.gif$/i', $img_file) == TRUE && (imageTypes() & IMG_GIF) == TRUE) { imageGif($dst_img, $cnvrtd_img); } imageDestroy($dst_img); $using = $cnvrt_mesgs['using GD'] . $gd_version; } if ($cnvrt_alt['mesg_on'] == TRUE && is_file($cnvrtd_img)) { $str .= " <small>\n" . ' ' . $cnvrt_mesgs['generated'] . $cnvrt_arry['txt'] . $cnvrt_mesgs['converted'] . $cnvrt_mesgs['image_for'] . $img_file . $using . ".\n" . " </small>\n <br />\n"; } } } } if (isset($str)) { return $str; } }
function Main($path, $width, $height, $dst_file, $header = false) { if (!isset($path)) { return array(0, "イメージのパスが設定されていません。"); } if (!file_exists($path)) { return array(0, "指定されたパスにファイルが見つかりません。"); } // 画像の大きさをセット if ($width) { $this->imgMaxWidth = $width; } if ($height) { $this->imgMaxHeight = $height; } $size = @GetimageSize($path); $re_size = $size; //アスペクト比固定処理 if ($this->imgMaxWidth != 0) { $tmp_w = $size[0] / $this->imgMaxWidth; } if ($this->imgMaxHeight != 0) { $tmp_h = $size[1] / $this->imgMaxHeight; } if ($tmp_w > 1 || $tmp_h > 1) { if ($this->imgMaxHeight == 0) { if ($tmp_w > 1) { $re_size[0] = $this->imgMaxWidth; $re_size[1] = $size[1] * $this->imgMaxWidth / $size[0]; } } else { if ($tmp_w > $tmp_h) { $re_size[0] = $this->imgMaxWidth; $re_size[1] = $size[1] * $this->imgMaxWidth / $size[0]; } else { $re_size[1] = $this->imgMaxHeight; $re_size[0] = $size[0] * $this->imgMaxHeight / $size[1]; } } } $imagecreate = function_exists("imagecreatetruecolor") ? "imagecreatetruecolor" : "imagecreate"; $imageresize = function_exists("imagecopyresampled") ? "imagecopyresampled" : "imagecopyresized"; switch ($size[2]) { // gif形式 case "1": if (function_exists("imagecreatefromgif")) { $src_im = imagecreatefromgif($path); $dst_im = $imagecreate($re_size[0], $re_size[1]); $transparent = imagecolortransparent($src_im); $colorstotal = imagecolorstotal($src_im); $dst_im = imagecreate($re_size[0], $re_size[1]); if (0 <= $transparent && $transparent < $colorstotal) { imagepalettecopy($dst_im, $src_im); imagefill($dst_im, 0, 0, $transparent); imagecolortransparent($dst_im, $transparent); } $imageresize($dst_im, $src_im, 0, 0, 0, 0, $re_size[0], $re_size[1], $size[0], $size[1]); if (function_exists("imagegif")) { // 画像出力 if ($header) { header("Content-Type: image/gif"); imagegif($dst_im); return ""; } else { $dst_file = $dst_file . ".gif"; if ($re_size[0] == $size[0] && $re_size[1] == $size[1]) { // サイズが同じ場合には、そのままコピーする。(画質劣化を防ぐ) copy($path, $dst_file); } else { imagegif($dst_im, $dst_file); } } imagedestroy($src_im); imagedestroy($dst_im); } else { // 画像出力 if ($header) { header("Content-Type: image/png"); imagepng($dst_im); return ""; } else { $dst_file = $dst_file . ".png"; if ($re_size[0] == $size[0] && $re_size[1] == $size[1]) { // サイズが同じ場合には、そのままコピーする。(画質劣化を防ぐ) copy($path, $dst_file); } else { imagepng($dst_im, $dst_file); } } imagedestroy($src_im); imagedestroy($dst_im); } } else { // サムネイル作成不可の場合(旧バージョン対策) $dst_im = imageCreate($re_size[0], $re_size[1]); imageColorAllocate($dst_im, 255, 255, 214); //背景色 // 枠線と文字色の設定 $black = imageColorAllocate($dst_im, 0, 0, 0); $red = imageColorAllocate($dst_im, 255, 0, 0); imagestring($dst_im, 5, 10, 10, "GIF {$size['0']}x{$size['1']}", $red); imageRectangle($dst_im, 0, 0, $re_size[0] - 1, $re_size[1] - 1, $black); // 画像出力 if ($header) { header("Content-Type: image/png"); imagepng($dst_im); return ""; } else { $dst_file = $dst_file . ".png"; imagepng($dst_im, $dst_file); } imagedestroy($src_im); imagedestroy($dst_im); } break; // jpg形式 // jpg形式 case "2": $src_im = imageCreateFromJpeg($path); $dst_im = $imagecreate($re_size[0], $re_size[1]); $imageresize($dst_im, $src_im, 0, 0, 0, 0, $re_size[0], $re_size[1], $size[0], $size[1]); // 画像出力 if ($header) { header("Content-Type: image/jpeg"); imageJpeg($dst_im); return ""; } else { $dst_file = $dst_file . ".jpg"; if ($re_size[0] == $size[0] && $re_size[1] == $size[1]) { // サイズが同じ場合には、そのままコピーする。(画質劣化を防ぐ) copy($path, $dst_file); } else { imageJpeg($dst_im, $dst_file); } } imagedestroy($src_im); imagedestroy($dst_im); break; // png形式 // png形式 case "3": $src_im = imageCreateFromPNG($path); $colortransparent = imagecolortransparent($src_im); $has_alpha = ord(file_get_contents($path, false, null, 25, 1)) & 0x4; if ($colortransparent > -1 || $has_alpha) { $dst_im = $imagecreate($re_size[0], $re_size[1]); // アルファチャンネルが存在する場合はそちらを使用する if ($has_alpha) { imagealphablending($dst_im, false); imagesavealpha($dst_im, true); } imagepalettecopy($dst_im, $src_im); imagefill($dst_im, 0, 0, $colortransparent); imagecolortransparent($dst_im, $colortransparent); imagecopyresized($dst_im, $src_im, 0, 0, 0, 0, $re_size[0], $re_size[1], $size[0], $size[1]); } else { $dst_im = $imagecreate($re_size[0], $re_size[1]); imagecopyresized($dst_im, $src_im, 0, 0, 0, 0, $re_size[0], $re_size[1], $size[0], $size[1]); imagecolorstotal($src_im) == 0 ? $colortotal = 65536 : ($colortotal = imagecolorstotal($src_im)); imagetruecolortopalette($dst_im, true, $colortotal); } // 画像出力 if ($header) { header("Content-Type: image/png"); imagepng($dst_im); return ""; } else { $dst_file = $dst_file . ".png"; if ($re_size[0] == $size[0] && $re_size[1] == $size[1]) { // サイズが同じ場合には、そのままコピーする。(画質劣化を防ぐ) copy($path, $dst_file); } else { imagepng($dst_im, $dst_file); } } imagedestroy($src_im); imagedestroy($dst_im); break; default: return array(0, "イメージの形式が不明です。"); } return array(1, $dst_file); }
/** * Creates a new GDlib image resource based on the input image filename. * If it fails creating an image from the input file a blank gray image with the dimensions of the input image will be created instead. * * @param string $sourceImg Image filename * @return resource Image Resource pointer */ public function imageCreateFromFile($sourceImg) { $imgInf = pathinfo($sourceImg); $ext = strtolower($imgInf['extension']); switch ($ext) { case 'gif': if (function_exists('imagecreatefromgif')) { return imageCreateFromGif($sourceImg); } break; case 'png': if (function_exists('imagecreatefrompng')) { $imageHandle = imageCreateFromPng($sourceImg); if ($this->saveAlphaLayer) { imagesavealpha($imageHandle, true); } return $imageHandle; } break; case 'jpg': case 'jpeg': if (function_exists('imagecreatefromjpeg')) { return imageCreateFromJpeg($sourceImg); } break; } // If non of the above: $i = @getimagesize($sourceImg); $im = imagecreatetruecolor($i[0], $i[1]); $Bcolor = ImageColorAllocate($im, 128, 128, 128); ImageFilledRectangle($im, 0, 0, $i[0], $i[1], $Bcolor); return $im; }
function getImageType($file) { //使用getimagesize()函数,返回图像相关数据 file_exists($file) ? $TempImage = getimagesize($file) : die("文件不存在!"); switch ($TempImage[2]) { case 1: $image = imageCreateFromGif($file); $this->_type = "gif"; break; case 2: $image = imageCreateFromJpeg($file); $this->_type = "jpg"; break; case 3: $image = imageCreateFromPng($file); $this->_type = "png"; break; default: die("不支持该文件格式,请使用GIF、JPG、PNG格式。"); } unset($TempImage); $this->_im = $image; }
$file_size = $_FILES['myfile']['size']; $sql = "select count(id)+1 from pe.file_allegati allegati where pratica={$idpratica} and allegato={$idallegato}"; if ($desc) { $sql = "insert into pe.file_allegati (pratica,allegato,nome_file,tipo_file,size_file,ordine,chk,uidins,tmsins,form,note) values ({$idpratica},{$idallegato},'{$file_name}','{$file_type}',{$file_size},({$sql}),1," . $_SESSION["USER_ID"] . "," . time() . ",'{$form}','{$desc}');"; } else { $sql = "insert into pe.file_allegati (pratica,allegato,nome_file,tipo_file,size_file,ordine,chk,uidins,tmsins,form) values ({$idpratica},{$idallegato},'{$file_name}','{$file_type}',{$file_size},({$sql}),1," . $_SESSION["USER_ID"] . "," . time() . ",'{$form}');"; } $db->sql_query($sql); $_SESSION["ADD_NEW"] = 1; if (DEBUG) { echo $sql; } //creo l'oggetto immagine se il file è tipo immagine if (ereg("jpeg", $file_type)) { $im_file_name = $updDir . $file_name; $image_attribs = getimagesize($im_file_name); $im_old = @imageCreateFromJpeg($im_file_name); //setto le dimensioni del thumbnail $th_max_width = 100; $th_max_height = 100; $ratio = $width > $height ? $th_max_width / $image_attribs[0] : $th_max_height / $image_attribs[1]; $th_width = $image_attribs[0] * $ratio; $th_height = $image_attribs[1] * $ratio; $im_new = imagecreatetruecolor($th_width, $th_height); //@imageantialias ($im_new,true); //creo la thumbnail e la copio sul disco $th_file_name = $updDir . "tmb/tmb_" . $file_name; @imageCopyResampled($im_new, $im_old, 0, 0, 0, 0, $th_width, $th_height, $image_attribs[0], $image_attribs[1]); @imageJpeg($im_new, $th_file_name, 100); ini_set('max_execution_time', $var); }
} exit; } if (!file_exists($image_path)) { //header("location: $path"); exit; } switch ($ext) { case "gif": $img = imageCreateFromGif($image_path); break; case "jpg": $img = imageCreateFromJpeg($image_path); break; case "jpeg": $img = imageCreateFromJpeg($image_path); break; case "png": $img = imageCreateFromPng($image_path); break; default: $img = null; break; } if ($img == null) { header("location: {$path}"); exit; } else { $orig_width = imageSX($img); $orig_height = imageSY($img); $image_width = $orig_width;
function create_image_from_any($filepath) { $type = exif_imagetype($filepath); // [] if you don't have exif you could use getImageSize() //echo $type; exit; $allowedTypes = array(1, 2, 3); if (!in_array($type, $allowedTypes)) { return false; } switch ($type) { case 1: $image = imageCreateFromGif($filepath); break; case 2: $image = imageCreateFromJpeg($filepath); break; case 3: $image = imageCreateFromPng($filepath); break; // case 6 : // $image = imageCreateFromBmp($filepath); // break; } return $image; }
function fn_makeImage($photographName, $selectionToPhotographRectangle = null, $zoom = 1, $width = null, $height = null, $filepath = null) { $imageSpecs = getimagesize($photographName); $extension = substr($photographName, -4); switch (strtolower($extension)) { case '.jpg': $image = imageCreateFromJpeg($photographName); break; case '.png': $image = imageCreateFrompng($photographName); break; case '.gif': $image = imageCreateFromGif($photographName); break; default: $image = imageCreateFromJpeg($photographName); break; } // try to split bounding box on comma ',' $coordiantesArray = split(",", $selectionToPhotographRectangle); if (!$coordiantesArray[1]) { // try to split on space ' ' $coordiantesArray = split(" ", $selectionToPhotographRectangle); } if (!$coordiantesArray[1]) { // still no split? use whole image $coordiantesArray = array(0, 0, $imageSpecs[0], $imageSpecs[1]); } // translate boundingbox's x1,y1,x2,y2 to x1,y1,w,h $fn_x1 = $coordiantesArray[0]; $fn_y1 = $coordiantesArray[1]; $fn_w = $coordiantesArray[2] - $coordiantesArray[0]; $fn_h = $coordiantesArray[3] - $coordiantesArray[1]; if (!$zoom) { // no zoom? give full size $zoom = 1; } displayDebug("selection: {$selectionToPhotographRectangle}", 3); displayDebug("\n{$fn_x1} | {$fn_y1} | {$fn_w} | {$fn_h} ", 3); // adjust for width and height params if passed if ($width && !$height) { $height = $width * $imageSpecs[1] / $imageSpecs[0]; } else { if (!$width) { $width = $fn_w * $zoom; } if (!$height) { $height = $fn_h * $zoom; } } // let's grabbed the region #$croppedImage=ImageCreate($width, $height); $croppedImage = imagecreatetruecolor($width, $height); imagecopyresized($croppedImage, $image, 0, 0, $coordiantesArray[0], $coordiantesArray[1], $width, $height, $fn_w, $fn_h); $image = $croppedImage; if ($filepath) { // output to file switch (strtolower($extension)) { // send to browser with proper header case '.jpg': imagejpeg($image, $filepath); break; case '.png': imagepng($image, $filepath); break; case '.gif': imageGif($image, $filepath); break; default: imagejpeg($image, $filepath); break; } } else { // output to browser switch (strtolower($extension)) { // send to browser with proper header case '.jpg': header('Content-Type: image/jpeg'); imagejpeg($image); break; case '.png': header('Content-Type: image/png'); imagepng($image); break; case '.gif': header('Content-Type: image/gif'); imageGif($image); break; default: header('Content-Type: image/jpeg'); imagejpeg($image); break; } } // delete image from memory imagedestroy($image); return true; }
// 枠線と文字色の設定 $black = imageColorAllocate($dst_im, 0, 0, 0); $red = imageColorAllocate($dst_im, 255, 0, 0); imagestring($dst_im, 5, 10, 10, "GIF $size[0]x$size[1]", $red); imageRectangle ($dst_im, 0, 0, ($re_size[0]-1), ($re_size[1]-1), $black); header("content-Type: image/png"); imagepng($dst_im); imagedestroy($src_im); imagedestroy($dst_im); } */ ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////// // jpg形式 case "2": $src_im = imageCreateFromJpeg($photo_path); $dst_im = $imagecreate($re_size[0], $re_size[1]); $imageresize($dst_im, $src_im, 0, 0, 0, 0, $re_size[0], $re_size[1], $size[0], $size[1]); header("content-Type: image/jpeg"); header("cache-control: no-cache"); imageJpeg($dst_im); imagedestroy($src_im); imagedestroy($dst_im); break; ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////// // png形式 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////// // png形式 case "3": $src_im = imagecreatefrompng($photo_path); $colortransparent = imagecolortransparent($src_im);
public static function convert($srcFile = null, $destFile = null, $width = 0, $height = 0, $mode = self::MODE_CUT) { if (false === file_exists($srcFile)) { return false; } preg_match('/\\.([^\\.]+)$/', $destFile, $matches); switch (strtolower($matches[1])) { case 'jpg': case 'jpeg': $saveType = self::TYPE_JPG; break; case 'gif': $saveType = self::TYPE_GIF; break; case 'png': $saveType = self::TYPE_PNG; break; default: $saveType = self::TYPE_JPG; } $type = self::getImageType($srcFile); $srcImage = null; switch ($type) { case self::TYPE_GIF: $srcImage = imageCreateFromGif($srcFile); break; case self::TYPE_JPG: $srcImage = imageCreateFromJpeg($srcFile); break; case self::TYPE_PNG: $srcImage = imageCreateFromPng($srcFile); break; default: return false; } $srcWidth = imageSX($srcImage); $srcHeight = imageSY($srcImage); if ($width == 0 && $height == 0) { $width = $srcWidth; $height = $srcHeight; $mode = self::MODE_SCALE; } else { if ($width > 0 & $height == 0) { $useWidth = true; $mode = self::MODE_SCALE; if ($srcWidth <= $width) { return self::saveFile($srcImage, $destFile, $saveType); } } else { if ($width == 0 && $height > 0) { $mode = self::MODE_SCALE; } } } if ($mode == self::MODE_SCALE) { if ($width > 0 & $height > 0) { $useWidth = $srcWidth * $height > $srcHeight * $width ? true : false; } if (isset($useWidth) && $useWidth == true) { $height = $srcHeight * $width / $srcWidth; } else { $width = $srcWidth * $height / $srcHeight; } } $destImage = imageCreateTrueColor($width, $height); if ($mode == self::MODE_CUT) { $useWidth = $srcWidth * $height > $srcHeight * $width ? false : true; if ($useWidth == true) { $tempWidth = $width; $tempHeight = $srcHeight * $tempWidth / $srcWidth; } else { $tempHeight = $height; $tempWidth = $srcWidth * $tempHeight / $srcHeight; } $tempImage = imageCreateTrueColor($tempWidth, $tempHeight); $srcImage = imageCopyResampled($tempImage, $srcImage, 0, 0, 0, 0, $tempWidth, $tempHeight, $srcWidth, $srcHeight); imageDestroy($srcImage); $srcImage = $tempImage; $srcWidth = $width; $srcHeight = $srcWidth * $width / $srcHeight; } if ($mode == self::MODE_SCALE) { imageCopyResampled($destImage, $srcImage, 0, 0, 0, 0, $width, $height, $srcWidth, $srcHeight); } else { imageCopyResampled($destImage, $srcImage, 0, 0, 0, 0, $width, $height, $width, $height); } @imageDestroy($srcImage); return self::saveFile($destImage, $destFile, $saveType); }
/** * Rotate an image (left or right) * * @param string $sImage Full path to the image * @param string $sCmd Command to perform. Must be "left" or "right" (without quotes) * @return mixed FALSE on failure, NULL on success */ public function rotate($sImage, $sCmd, $sActualFile = null) { if (!$this->_load($sImage)) { return false; } switch ($this->_aInfo[2]) { case 1: $hFrm = @imageCreateFromGif($this->sPath); break; case 3: $hFrm = @imageCreateFromPng($this->sPath); break; default: $hFrm = @imageCreateFromJpeg($this->sPath); break; } if (substr($this->sPath, 0, 7) != 'http://') { @unlink($this->sPath); } if (function_exists('imagerotate')) { if ($sCmd == 'left') { $im2 = imagerotate($hFrm, 90, 0); } else { $im2 = imagerotate($hFrm, 270, 0); } } else { $wid = imagesx($hFrm); $hei = imagesy($hFrm); $im2 = imagecreatetruecolor($hei, $wid); switch ($this->sType) { case 'jpeg': case 'jpg': case 'jpe': imagealphablending($im2, true); break; case 'png': //imagealphablending($im2, false); //imagesavealpha($im2, true); break; } for ($i = 0; $i < $wid; $i++) { for ($j = 0; $j < $hei; $j++) { $ref = imagecolorat($hFrm, $i, $j); if ($sCmd == 'right') { imagesetpixel($im2, $hei - 1 - $j, $i, $ref); } else { imagesetpixel($im2, $j, $wid - $i, $ref); } } } } switch ($this->sType) { case 'gif': @imagegif($im2, $this->sPath); break; case 'png': imagealphablending($im2, false); imagesavealpha($im2, true); @imagepng($im2, $this->sPath); break; default: @imagejpeg($im2, $this->sPath); break; } imagedestroy($hFrm); imagedestroy($im2); if (Phpfox::getParam('core.allow_cdn')) { Phpfox::getLib('cdn')->put($this->sPath, $sActualFile); } }
/** Cắt ảnh theo cỡ tùy chọn */ function create_image($args) { $hmdb = new MySQL(true, DB_NAME, DB_HOST, DB_USER, DB_PASSWORD, DB_CHARSET); hook_filter('before_create_image', $args); hook_action('create_image'); if (!is_array($args)) { parse_str($args, $args); } $file_id = $args['file']; if (isset_image($file_id)) { $row = get_file_data($file_id); $file_info = json_decode($row->file_info); $file_dst_name_body = $file_info->file_dst_name_body; $file_dst_name_ext = $file_info->file_dst_name_ext; $source_file = get_file_location($file_id); $crop_name = $file_dst_name_body . '-' . $args['w'] . 'x' . $args['h'] . '.' . $file_dst_name_ext; if (file_exists(get_file_location($file_id, FALSE) . $crop_name)) { return get_file_url($file_id, FALSE) . $crop_name; } else { /** resize file */ /* fix func exif_imagetype not avaiable */ $type = getimagesize($source_file); $type = $type['mime']; switch ($type) { case 'image/png': $type = IMAGETYPE_PNG; break; case 'image/jpeg': $type = IMAGETYPE_JPEG; break; case 'image/gif': $type = IMAGETYPE_GIF; break; case 'image/bmp': $type = IMAGETYPE_BMP; break; case 'image/x-ms-bmp': $type = IMAGETYPE_BMP; break; } /* fix func exif_imagetype not avaiable */ switch ($type) { case 1: $source = imageCreateFromGif($source_file); break; case 2: $source = imageCreateFromJpeg($source_file); break; case 3: $source = imageCreateFromPng($source_file); break; case 6: $source = imageCreateFromBmp($source_file); break; } /** resize file gốc về cùng 1 cỡ */ $size = getimagesize($source_file); $source_width = $size[0]; $source_height = $size[1]; $fix_width = $args['w']; $fix_height = $args['h']; $thumb = imagecreatetruecolor($fix_width, $fix_height); /* Fix black background */ $white = imagecolorallocate($thumb, 255, 255, 255); imagefill($thumb, 0, 0, $white); /* Fix black background */ /* fix quality with imagecopyresampled , repalce imagecopyresized */ imagecopyresampled($thumb, $source, 0, 0, 0, 0, $fix_width, $fix_height, $source_width, $source_height); $saveto = get_file_location($file_id, FALSE) . $crop_name; imagejpeg($thumb, $saveto, 100); return get_file_url($file_id, FALSE) . $crop_name; } } else { return FALSE; } }
<?php ## Увеличение картинки со сглаживанием. $from = imageCreateFromJpeg("sample2.jpg"); $to = imageCreateTrueColor(2000, 2000); imageCopyResampled($to, $from, 0, 0, 0, 0, imageSX($to), imageSY($to), imageSX($from), imageSY($from)); header("Content-type: image/jpeg"); imageJpeg($to);
} } $file = array_pop($path); $path = implode('/', $path); $ext = strtolower(pathinfo($file, PATHINFO_EXTENSION)); $src_actions = substr(QUERY, $sep_pos + 1); parse_str($src_actions, $actions); $src_path = ($path ? $path . '/' : NULL) . $file; if (!is_file(DIR_BASE . $src_path)) { return; } // Rotate before EXIF data if ($ext == 'jpg' || $ext == 'jpeg') { $exif = @exif_read_data(DIR_BASE . $src_path); // file may be not a .jpg and this will raise an error $img = @imageCreateFromJpeg(DIR_BASE . $src_path); if ($img && $exif && isset($exif['Orientation'])) { $ort = $exif['Orientation']; if ($ort == 6 || $ort == 5) { $img = imagerotate($img, 270, null); } if ($ort == 3 || $ort == 4) { $img = imagerotate($img, 180, null); } if ($ort == 8 || $ort == 7) { $img = imagerotate($img, 90, null); } if ($ort == 5 || $ort == 4 || $ort == 7) { imageflip($img, IMG_FLIP_HORIZONTAL); } imageJPEG($img, DIR_BASE . $src_path, 100);