/** * Agrega una marca de agua a la foto * @param string $absolutePath */ private function _addWaterMark($absolutePath) { $DOC_ROOT = $this->_CI->input->server('DOCUMENT_ROOT') . "/"; if (FALSE === is_file($absolutePath)) { return FALSE; } switch (TRUE) { case stristr($absolutePath, 'jpg'): $photoImage = ImageCreateFromJpeg("{$absolutePath}"); break; case stristr($absolutePath, 'gif'): $photoImage = ImageCreateFromGIF("{$absolutePath}"); break; case stristr($absolutePath, 'png'): $photoImage = ImageCreateFromPNG("{$absolutePath}"); break; } ImageAlphaBlending($photoImage, true); // Añadimos aquà el fichero de marca de agua. $logoImage = ImageCreateFromPNG($DOC_ROOT . "assets/imagenes/marca_agua_telam.png"); $logoW = ImageSX($logoImage); $logoH = ImageSY($logoImage); $tamanox = imagesx($photoImage); $ubicacionX = ($tamanox - $logoW) / 2; $tamanoy = imagesy($photoImage); $ubicacionY = ($tamanoy - $logoH) / 2; ImageCopy($photoImage, $logoImage, $ubicacionX, $ubicacionY, 0, 0, $logoW, $logoH); imagejpeg($photoImage, $absolutePath); ImageDestroy($photoImage); ImageDestroy($logoImage); }
function resize($src, $dest, $new_width, $new_height) { if (!file_exists($src)) { die('No source to sample picture: ' . $src); } $image_big = ImageCreateFromJpeg($src); $height = imageSY($image_big); $width = imageSX($image_big); $ratio_hor = 1; $ratio_ver = 1; if ($height > $new_height) { $ratio_ver = $new_height / $height; } if ($width > $new_width) { $ratio_hor = $new_width / $width; } $ratio = min($ratio_hor, $ratio_ver); $new_height = round($height * $ratio); $new_width = round($width * $ratio); $image_redim = imagecreatetruecolor($new_width, $new_height); #l'apercu de l'image (plus petite) imagecopyresampled($image_redim, $image_big, 0, 0, 0, 0, $new_width, $new_height, $width, $height); imagejpeg($image_redim, $dest, IMAGE_REDUCED_QUALITY); #ecrit l'apercu sur le disque }
function SetVar($srcFile, $echoType) { if (!file_exists($srcFile)) { echo '源图片文件不存在!'; exit; } $this->srcFile = $srcFile; $this->echoType = $echoType; $info = ""; $data = GetImageSize($this->srcFile, $info); switch ($data[2]) { case 1: if (!function_exists("imagecreatefromgif")) { echo "你的GD库不能使用GIF格式的图片,请使用Jpeg或PNG格式!<a href='javascript:go(-1);'>返回</a>"; exit; } $this->im = ImageCreateFromGIF($this->srcFile); break; case 2: if (!function_exists("imagecreatefromjpeg")) { echo "你的GD库不能使用jpeg格式的图片,请使用其它格式的图片!<a href='javascript:go(-1);'>返回</a>"; exit; } $this->im = ImageCreateFromJpeg($this->srcFile); break; case 3: $this->im = ImageCreateFromPNG($this->srcFile); break; } $this->srcW = ImageSX($this->im); $this->srcH = ImageSY($this->im); }
function get_hex($location, $extensions = array('PNG', 'png', 'Png', 'JPG', 'jpg', 'Jpg', 'JPEG', 'jpeg', 'Jpeg', 'GIF', 'gif', 'Gif'), $postvar = "myimage", $getvar = "imgclix") { if (isset($_GET[$getvar])) { foreach ($extensions as $var) { if (file_exists($location . str_replace(array("..", ".", $var), '', html_entity_decode($_GET[$getvar])) . "." . $var)) { if (stristr($var, 'png')) { $im = ImageCreateFromPng($location . str_replace(array("..", ".", $var), '', html_entity_decode($_GET[$getvar])) . "." . $var); } elseif (stristr($var, 'gif')) { $im = ImageCreateFromGIF($location . str_replace(array("..", ".", $var), '', html_entity_decode($_GET[$getvar])) . "." . $var); } elseif (stristr($var, 'jpg') || stristr($var, 'jpeg')) { $im = ImageCreateFromJpeg($location . str_replace(array("..", ".", $var), '', html_entity_decode($_GET[$getvar])) . "." . $var); } else { return FALSE; } $rgb = ImageColorAt($im, $_POST[$postvar . '_x'], $_POST[$postvar . '_y']); $rgb = imagecolorsforindex($im, $rgb); $hex = sprintf('#%02X%02X%02X', $rgb['red'], $rgb['green'], $rgb['blue']); break; } } } else { return FALSE; } if (!isset($hex) || $hex == '') { return FALSE; } return $hex; }
public function action_add_image() { if (count($_FILES)) { $pathinfo = pathinfo($_FILES['file']['name']); if (strtolower($pathinfo['extension']) == 'jpg') { $filename = URL::title($pathinfo['filename']) . '.jpg'; $new_filename = $filename; $counter = 1; while (!Content_Image::image_name_available($new_filename)) { $new_filename = substr($filename, 0, strlen($filename) - 4) . '_' . $counter . '.jpg'; $counter++; } if (move_uploaded_file($_FILES['file']['tmp_name'], APPPATH . '/user_content/images/' . $new_filename)) { $gd_img_object = ImageCreateFromJpeg(Kohana::$config->load('user_content.dir') . '/images/' . $new_filename); $details = array('width' => array(imagesx($gd_img_object)), 'height' => array(imagesy($gd_img_object))); foreach ($_POST['tag'] as $nr => $tag_name) { if ($tag_name != '') { if (!isset($details[$tag_name])) { $details[$tag_name] = array(); } $details[$tag_name][] = $_POST['tag_value'][$nr]; } } Content_Image::new_image($new_filename, $details); $this->add_message('Image "' . $new_filename . '" added'); } else { $this->add_error('Unknown error uploading image'); } } else { $this->add_error('Image must be of jpeg type (file extension .jpg)'); } } }
function SetVar($srcFile, $echoType) { $this->srcFile = $srcFile; $this->echoType = $echoType; $info = ''; $data = GetImageSize($this->srcFile, $info); switch ($data[2]) { case 1: if (!function_exists('imagecreatefromgif')) { exit; } $this->im = ImageCreateFromGIF($this->srcFile); break; case 2: if (!function_exists('imagecreatefromjpeg')) { exit; } $this->im = ImageCreateFromJpeg($this->srcFile); break; case 3: $this->im = ImageCreateFromPNG($this->srcFile); break; } $this->srcW = ImageSX($this->im); $this->srcH = ImageSY($this->im); }
function ImageResize($srcFile, $toW, $toH, $toFile = "") { if ($toFile == "") { $toFile = $srcFile; } $info = ""; $data = GetImageSize($srcFile, $info); switch ($data[2]) { case 1: if (!function_exists("imagecreatefromgif")) { echo "你的GD库不能使用GIF格式的图片,请使用Jpeg或PNG格式!<a href='javascript:go(-1);'>返回</a>"; exit; } $im = ImageCreateFromGIF($srcFile); break; case 2: if (!function_exists("imagecreatefromjpeg")) { echo "你的GD库不能使用jpeg格式的图片,请使用其它格式的图片!<a href='javascript:go(-1);'>返回</a>"; exit; } $im = ImageCreateFromJpeg($srcFile); break; case 3: $im = ImageCreateFromPNG($srcFile); break; } $srcW = ImageSX($im); $srcH = ImageSY($im); $toWH = $toW / $toH; $srcWH = $srcW / $srcH; if ($toWH <= $srcWH) { $ftoW = $toW; $ftoH = $ftoW * ($srcH / $srcW); } else { $ftoH = $toH; $ftoW = $ftoH * ($srcW / $srcH); } if ($srcW > $toW || $srcH > $toH) { if (function_exists("imagecreatetruecolor")) { @($ni = ImageCreateTrueColor($ftoW, $ftoH)); if ($ni) { ImageCopyResampled($ni, $im, 0, 0, 0, 0, $ftoW, $ftoH, $srcW, $srcH); } else { $ni = ImageCreate($ftoW, $ftoH); ImageCopyResized($ni, $im, 0, 0, 0, 0, $ftoW, $ftoH, $srcW, $srcH); } } else { $ni = ImageCreate($ftoW, $ftoH); ImageCopyResized($ni, $im, 0, 0, 0, 0, $ftoW, $ftoH, $srcW, $srcH); } if (function_exists('imagejpeg')) { ImageJpeg($ni, $toFile); } else { ImagePNG($ni, $toFile); } ImageDestroy($ni); } ImageDestroy($im); }
function pcs_href_image($src_path) { $strRet = DIR_WS_IMAGES . 'pcs_images/' . basename($src_path) . '_' . MODULE_ADDONS_PCSLIDESHOW_MAX_IMAGE_HEIGHT . '_' . MODULE_ADDONS_PCSLIDESHOW_MAX_IMAGE_WIDTH . '_' . MODULE_ADDONS_PCSLIDESHOW_IMAGE_QUALITY . '.jpg'; #This will be the filename of the resized image. if (!file_exists($strRet)) { #Create the file if it does not exist #check to see if source file exists if (!file_exists($src_path)) { return 'error1'; #check to see if source file is readable } elseif (!is_readable($src_path)) { return 'error2'; } #check if gif if (stristr(strtolower($src_path), '.gif')) { $oldImage = ImageCreateFromGif($src_path); } elseif (stristr(strtolower($src_path), '.jpg') || stristr(strtolower($src_path), '.jpeg')) { $oldImage = ImageCreateFromJpeg($src_path); } elseif (stristr(strtolower($src_path), '.png')) { $oldImage = ImageCreateFromPng($src_path); } else { return 'error3'; } #Create the new image if (function_exists("ImageCreateTrueColor")) { $newImage = ImageCreateTrueColor(MODULE_ADDONS_PCSLIDESHOW_MAX_IMAGE_WIDTH, MODULE_ADDONS_PCSLIDESHOW_MAX_IMAGE_HEIGHT); } else { $newImage = ImageCreate(MODULE_ADDONS_PCSLIDESHOW_MAX_IMAGE_WIDTH, MODULE_ADDONS_PCSLIDESHOW_MAX_IMAGE_HEIGHT); } $backgroundColor = imagecolorallocate($newImage, 255, 255, 255); imagefill($newImage, 0, 0, $backgroundColor); #calculate the rezised image's dimmensions if (imagesx($oldImage) > MODULE_ADDONS_PCSLIDESHOW_MAX_IMAGE_WIDTH || imagesy($oldImage) > MODULE_ADDONS_PCSLIDESHOW_MAX_IMAGE_HEIGHT) { #Resize image if (imagesx($oldImage) / MODULE_ADDONS_PCSLIDESHOW_MAX_IMAGE_WIDTH > imagesy($oldImage) / MODULE_ADDONS_PCSLIDESHOW_MAX_IMAGE_HEIGHT) { #Width is leading in beeing to large $newWidth = (int) MODULE_ADDONS_PCSLIDESHOW_MAX_IMAGE_WIDTH; $newHeight = (int) MODULE_ADDONS_PCSLIDESHOW_MAX_IMAGE_WIDTH / imagesx($oldImage) * imagesy($oldImage); } else { #Height is leading in beeing to large $newHeight = (int) MODULE_ADDONS_PCSLIDESHOW_MAX_IMAGE_HEIGHT; $newWidth = (int) MODULE_ADDONS_PCSLIDESHOW_MAX_IMAGE_HEIGHT / imagesy($oldImage) * imagesx($oldImage); } } else { #Don't rezise image $newWidth = imagesx($oldImage); $newHeight = imagesy($oldImage); } #Copy the old image onto the new image ImageCopyResampled($newImage, $oldImage, MODULE_ADDONS_PCSLIDESHOW_MAX_IMAGE_WIDTH / 2 - $newWidth / 2, MODULE_ADDONS_PCSLIDESHOW_MAX_IMAGE_HEIGHT / 2 - $newHeight / 2, 0, 0, $newWidth, $newHeight, imagesx($oldImage), imagesy($oldImage)); imagejpeg($newImage, $strRet, MODULE_ADDONS_PCSLIDESHOW_IMAGE_QUALITY); #save the image imagedestroy($oldImage); #Free Memory imagedestroy($newImage); #Free memory } return $strRet; }
function ResizeImage($Filename, $Thumbnail, $Size) { $Path = pathinfo($Filename); $Extension = $Path['extension']; $ImageData = @GetImageSize($Filename); $Width = $ImageData[0]; $Height = $ImageData[1]; if ($Width >= $Height and $Width > $Size) { $NewWidth = $Size; $NewHeight = $Size / $Width * $Height; } elseif ($Height >= $Width and $Height > $Size) { $NewWidth = $Size / $Height * $Width; $NewHeight = $Size; } else { $NewWidth = $Width; $NewHeight = $Height; } $NewImage = @ImageCreateTrueColor($NewWidth, $NewHeight); if (preg_match('/^gif$/i', $Extension)) { $Image = @ImageCreateFromGif($Filename); } elseif (preg_match('/^png$/i', $Extension)) { $Image = @ImageCreateFromPng($Filename); } else { $Image = @ImageCreateFromJpeg($Filename); } if ($ImageData[2] == IMAGETYPE_GIF or $ImageData[2] == IMAGETYPE_PNG) { $TransIndex = imagecolortransparent($Image); // If we have a specific transparent color if ($TransIndex >= 0) { // Get the original image's transparent color's RGB values $TransColor = imagecolorsforindex($Image, $TransIndex); // Allocate the same color in the new image resource $TransIndex = imagecolorallocate($NewImage, $TransColor['red'], $TransColor['green'], $TransColor['blue']); // Completely fill the background of the new image with allocated color. imagefill($NewImage, 0, 0, $TransIndex); // Set the background color for new image to transparent imagecolortransparent($NewImage, $TransIndex); } elseif ($ImageData[2] == IMAGETYPE_PNG) { // Turn off transparency blending (temporarily) imagealphablending($NewImage, false); // Create a new transparent color for image $color = imagecolorallocatealpha($NewImage, 0, 0, 0, 127); // Completely fill the background of the new image with allocated color. imagefill($NewImage, 0, 0, $color); // Restore transparency blending imagesavealpha($NewImage, true); } } @ImageCopyResampled($NewImage, $Image, 0, 0, 0, 0, $NewWidth, $NewHeight, $Width, $Height); if (preg_match('/^gif$/i', $Extension)) { @ImageGif($NewImage, $Thumbnail); } elseif (preg_match('/^png$/i', $Extension)) { @ImagePng($NewImage, $Thumbnail); } else { @ImageJpeg($NewImage, $Thumbnail); } @chmod($Thumbnail, 0644); }
public static function createFromFile($filename, $mime_type, $objAlbum) { $objPicture = new clsPicture(); /* Decide which incoming mime type it is. */ switch ($mime_type) { case 'image/jpeg': $img = ImageCreateFromJpeg($filename); break; case 'image/png': $img = ImageCreateFromPng($filename); break; case 'image/gif': $img = ImageCreateFromGif($filename); break; default: return 'image_filetype'; } list($intWidth, $intHeight) = getImageSize($filename); $intMaxWidth = $objAlbum->get('max_width'); $intMaxHeight = $objAlbum->get('max_height'); if ($intMaxWidth <= 0) { $intMaxWidth = DEFAULT_X; } if ($intMaxHeight <= 0) { $intMaxHeight = DEFAULT_Y; } if ($intWidth > $intMaxWidth || $intHeight > $intMaxHeight) { /* Check whether the image needs to be resized vertically or horizonally more. */ if ($intWidth / $intMaxWidth > $intHeight / $intMaxHeight) { /* Right-left needs to have priority. */ $ratio = $intMaxWidth / $intWidth; } else { /* Up-down needs to have priority. */ $ratio = $intMaxHeight / $intHeight; } $intNewWidth = $intWidth * $ratio; $intNewHeight = $intHeight * $ratio; $imgNew = @ImageCreateTrueColor($intNewWidth, $intNewHeight); if (!@ImageCopyResized($imgNew, $img, 0, 0, 0, 0, $intNewWidth, $intNewHeight, $intWidth, $intHeight)) { return "image_noresize"; } $intWidth = $intNewWidth; $intHeight = $intNewHeight; ImageDestroy($img); $img = $imgNew; } /* This has to be done before setImage() because setImage() needs data from the album. */ $objPicture->set('album_id', $objAlbum->get('id')); $result = $objPicture->setImage($img); ImageDestroy($img); if ($result) { return $result; } $objPicture->set('width', $intWidth); $objPicture->set('height', $intHeight); $objPicture->save(); return $objPicture; }
/** * * @throws Sitengine_Exception * */ public static function resizeJpeg($inFile, $outFile, $length, $method, $mode = 0644, $quality = 50) { $size = getimagesize($inFile); if (!$size) { throw new Sitengine_Exception('jpg could not be opened'); } if ($size[2] != 2) { throw new Sitengine_Exception('image is not a jpg'); } if (!preg_match('/^\\d{1,3}$/', $quality)) { $quality = 50; } if ($quality < 0 || $quality > 100) { $quality = 50; } switch ($method) { case 'width': $width = $length; $height = self::calcHeight($size[0], $size[1], $length); break; case 'height': $width = self::calcWidth($size[0], $size[1], $length); $height = $length; break; default: if ($size[0] > $size[1]) { $width = $length; $height = self::calcHeight($size[0], $size[1], $length); } else { $width = self::calcWidth($size[0], $size[1], $length); $height = $length; } break; } $inRid = ImageCreateFromJpeg($inFile); if (!$inRid) { throw new Sitengine_Exception('jpg processing error'); } $outRid = imagecreatetruecolor($width, $height); if (!$outRid) { throw new Sitengine_Exception('jpg processing error'); } $copy = imagecopyresampled($outRid, $inRid, 0, 0, 0, 0, $width, $height, $size[0], $size[1]); if (!$copy) { throw new Sitengine_Exception('jpg processing error'); } $image = imagejpeg($outRid, $outFile, $quality); if (!$image) { throw new Sitengine_Exception('jpg processing error'); } chmod($outFile, $mode); $stats = stat($outFile); return array('mime' => 'image/jpeg', 'size' => $stats['size'], 'width' => $width, 'height' => $height); }
function createImage() { $imageString = $_GET['type']; $length = strlen($imageString); $max = 6; $x = ($max - $length) * 7; $image = ImageCreateFromJpeg("../images/file_manager_file.jpg"); $black = ImageColorAllocate($image, 0, 0, 0); ImageString($image, 6, $x, 45, $imageString, $black); header("Content-Type: image/jpeg"); ImageJpeg($image); ImageDestroy($image); }
private function createimageByType($data, $file) { if ($data['type'] === "image/jpeg") { $image = ImageCreateFromJpeg($file); } if ($data['type'] === "image/png") { $image = ImageCreateFromPng($file); } if ($data['type'] === "image/gif") { $image = ImageCreateFromGif($file); } return $image; }
private function create_image_container($file, $type) { $path = $this->config->item("upload_dir"); if (strtolower($type) === ".jpg" || strtolower($type) === ".jpeg") { $image = ImageCreateFromJpeg($path . $file . $type); } elseif (strtolower($type) === ".png") { $image = ImageCreateFromPng($path . $file . $type); } elseif (strtolower($type) === ".gif") { $image = ImageCreateFromGif($path . $file . $type); } else { $image = false; } return $image; }
function grip($K, $OldImageFile, $NewImageFile) { $WidthAndHeight = getimagesize($OldImageFile); $Width = $WidthAndHeight[0]; $Height = $WidthAndHeight[1]; $NewWidth = ceil($Width * $K); $NewHeight = ceil($Height * $K); $src = ImageCreateFromJpeg($OldImageFile); $dst = imagecreatetruecolor($NewWidth, $NewHeight); ImageCopyResampled($dst, $src, 0, 0, 0, 0, $NewWidth, $NewHeight, $Width, $Height); ImageJpeg($dst, $NewImageFile, 80); ImageDestroy($src); ImageDestroy($dst); }
function ImageCreateFromType($type,$filename) { $im = null; switch ($type) { case 1: $im = ImageCreateFromGif($filename); break; case 2: $im = ImageCreateFromJpeg($filename); break; case 3: $im = ImageCreateFromPNG($filename); break; } return $im; }
function image_resize($upfile, $output_filename, $output_path, $dst_w = 100, $dst_h = 100, $isRadio = 1, $trans_color = array(254, 254, 254)) { $imagedata = GetImageSize($upfile); // Read the size $src_w = $imagedata[0]; $src_h = $imagedata[1]; $src_type = $imagedata[2]; $re_dst_x = 0; $re_dst_y = 0; if ($isRadio | 0 > 0) { if ($dst_w >= $src_w && $dst_h >= $src_h) { $re_dst_w = $src_w; $re_dst_h = $src_h; } else { $p_w = $dst_w / $src_w; $p_h = $dst_h / $src_h; $p = min($p_w, $p_h); $re_dst_w = $src_w * $p; $re_dst_h = $src_h * $p; } } else { $re_dst_w = $dst_w; $re_dst_h = $dst_h; } if ($src_type == 1) { $src_image = ImageCreateFromGif($upfile); } else { if ($src_type == 2) { $src_image = ImageCreateFromJpeg($upfile); } else { if ($src_type == 3) { $src_image = ImageCreateFromPng($upfile); } else { if ($src_type == 16) { $src_image = imagecreatefromxbm($upfile); } } } } //else if ($src_type==6) // return; $dst_image = imagecreatetruecolor($re_dst_w, $re_dst_h); $bgc = imagecolorallocate($dst_image, $trans_color[0], $trans_color[1], $trans_color[2]); imagefilledrectangle($dst_image, 0, 0, $re_dst_w, $re_dst_h, $bgc); imagecolortransparent($dst_image, $bgc); imagecopyresampled($dst_image, $src_image, $re_dst_x, $re_dst_y, 0, 0, $re_dst_w, $re_dst_h, $src_w, $src_h); imagepng($dst_image, $output_path . $output_filename); }
public function __construct() { parent::__construct(); if (Kohana::$environment == Kohana::DEVELOPMENT) { if (!$this->check_db_structure()) { $this->create_db_structure(); $this->insert_initial_data(); } } // Add image files that does not exist in database $tracked_images = $this->get_images(NULL, array(), TRUE); $cwd = getcwd(); chdir(Kohana::$config->load('user_content.dir') . '/images'); $actual_images = glob('*.jpg'); chdir($cwd); foreach (array_diff($actual_images, $tracked_images) as $non_tracked_image) { $pathinfo = pathinfo($non_tracked_image); if ($pathinfo['filename'] != URL::title($pathinfo['filename'])) { // We need to rename the image so it fits the URL $new_filename = URL::title($pathinfo['filename']); while (!Content_Image::image_name_available($new_filename)) { $new_filename = URL::title($pathinfo['filename']) . '_' . $counter . '.jpg'; $counter++; } copy(Kohana::$config->load('user_content.dir') . '/images/' . $non_tracked_image, Kohana::$config->load('user_content.dir') . '/images/' . $new_filename . '.jpg'); @unlink(Kohana::$config->load('user_content.dir') . '/images/' . $non_tracked_image); $non_tracked_image = $new_filename . '.jpg'; } if (@($gd_img_object = ImageCreateFromJpeg(Kohana::$config->load('user_content.dir') . '/images/' . $non_tracked_image))) { $gd_img_object = ImageCreateFromJpeg(Kohana::$config->load('user_content.dir') . '/images/' . $non_tracked_image); $width = imagesx($gd_img_object); $height = imagesy($gd_img_object); $this->new_image($non_tracked_image, array('width' => $width, 'height' => $height)); } else { // This jpg file is not a valid jpg image $path_parts = pathinfo($non_tracked_image); $new_filename = $path_parts['filename'] . '.broken_jpg'; while (file_exists(Kohana::$config->load('user_content.dir') . '/images/' . $new_filename)) { if (!isset($counter)) { $counter = 1; } $new_filename = $path_parts['filename'] . '_' . $counter . '.broken_jpg'; $counter++; } @rename(Kohana::$config->load('user_content.dir') . '/images/' . $non_tracked_image, Kohana::$config->load('user_content.dir') . '/images/' . $new_filename); } } }
private function getImage($imageUrl) { if (!file_get_contents($imageUrl)) { throw new \Exception("Image not found"); } $ext = strtolower(array_pop(explode('.', $imageUrl))); if ($ext == "jpeg" || $ext == "jpg") { return ImageCreateFromJpeg($imageUrl); } elseif ($ext == "gif") { return ImageCreateFromGif($imageUrl); } elseif ($ext == "png") { return ImageCreateFromPng($imageUrl); } else { throw new \Exception("Image not supported"); } }
public function action_index() { // Remove an image if (isset($_GET['rm'])) { $image = new Content_Image($_GET['rm']); $image->rm_image(); $this->redirect(); } // Add image if (isset($_POST['upload_image'])) { foreach ($_FILES['image']['name'] as $nr => $name) { if ($name != '') { $pathinfo = pathinfo($_FILES['image']['name'][$nr]); if (strtolower($pathinfo['extension']) == 'jpg' || strtolower($pathinfo['extension']) == 'png') { $filename = 'frontimage.' . strtolower($pathinfo['extension']); $new_filename = $filename; $counter = 1; while (!Content_Image::image_name_available($new_filename)) { $new_filename = substr($filename, 0, strlen($filename) - 4) . '_' . $counter . '.' . strtolower($pathinfo['extension']); $counter++; } if (move_uploaded_file($_FILES['image']['tmp_name'][$nr], APPPATH . '/user_content/images/' . $new_filename)) { if (strtolower($pathinfo['extension']) == 'jpg') { $gd_img_object = ImageCreateFromJpeg(Kohana::$config->load('user_content.dir') . '/images/' . $new_filename); } elseif (strtolower($pathinfo['extension']) == 'png') { $gd_img_object = ImageCreateFromPng(Kohana::$config->load('user_content.dir') . '/images/' . $new_filename); } $details = array('width' => array(imagesx($gd_img_object)), 'height' => array(imagesy($gd_img_object)), 'frontimage' => NULL); Content_Image::new_image($new_filename, $details); } else { $this->add_error('Unknown error uploading image(s)'); } } } } } // Images $images = array(); foreach (Content_image::get_images(NULL, array('frontimage' => TRUE)) as $image_name => $image_data) { $images[] = array('name' => $image_name); } $this->xml_content_images = $this->xml_content->appendChild($this->dom->createElement('images')); xml::to_XML($images, $this->xml_content_images, 'image'); }
function create($data) { $this->destroy(); if (file_exists($data)) { $this->img = @ImageCreateFromJpeg($data); $this->fileInfo = basename($data); $this->fullName = $data; } else { $this->img = @ImageCreateFromString($data); } if (!$this->img) { $this->destroy(); return false; } else { $this->orgX = ImageSX($this->img); $this->orgY = ImageSY($this->img); return true; } }
public function __construct($source) { $extension = false; if (is_array($source) && !empty($source['tmp_name']) && !empty($source['type'])) { $this->originalImagePath = $source['tmp_name']; $extensionByType = array('image/jpeg' => 'jpg', 'image/png' => 'png'); if (isset($extensionByType[$source['type']])) { $extension = $extensionByType[$source['type']]; } else { $this->errors[] = 'Error: Http posted image must be a jpeg or png to use this class.'; return $this; } } elseif (is_resource($source) && get_resource_type($source) == 'gd') { $this->originalImage = $source; } elseif (file_exists($source)) { $this->originalImagePath = $source; $this->originalImageFileInfos = pathinfo($this->originalImagePath); //d($fInfos); exit(); $extension = strtolower($this->originalImageFileInfos['extension']); if (!in_array($extension, array('jpeg', 'jpg', 'png'))) { $this->errors[] = 'Error: Image must be a jpeg or png to use this class.'; return $this; } } else { $this->errors[] = 'Error: Invalide source. Source must be either an image filepath or an http posted image file.'; return $this; } if ($extension) { $this->originalImage = $extension == 'png' ? imagecreatefrompng($this->originalImagePath) : ImageCreateFromJpeg($this->originalImagePath); } //$originalImageSize = getimagesize($this->originalImagePath); //d($this->originalImageSize, 'originalImageSize'); //exit(); $this->originalImageWidth = imagesx($this->originalImage); //$originalImageSize[0]; $this->originalImageHeight = imagesy($this->originalImage); //$originalImageSize[1]; $this->originalImageRatio = $this->originalImageWidth / $this->originalImageHeight; //d($this->originalImageRatio, 'originalImageRatio'); return $this; }
private function resizeJpeg($newW, $newH, $oWidth, $oHeight, $fullPath) { // create a new canvas $im = ImageCreateTruecolor($newW, $newH); $baseImage = ImageCreateFromJpeg($this->imageName); // if successful this returns as true - this is the actual resizing if (imagecopyresampled($im, $baseImage, 0, 0, 0, 0, $newW, $newH, $oWidth, $oHeight)) { // resizing is successful and saved to $fullPath imageJpeg($im, $fullPath); if (file_exists($fullPath)) { $this->msg .= 'Resized file created<br />'; imagedestroy($im); // don't really need this cos $im will disappear after function done return true; } else { $this->msg .= 'Failure in resize jpeg<br />'; } } else { // resizing fails $this->msg .= 'Unable to resize image<br />'; return false; } }
public function toPng($src, $destroyOriginal = false) { $parts = explode('.', $src); $fileExtension = strtolower(array_pop($parts)); if ($fileExtension == 'png') { return $src; } if ($fileExtension != 'png') { $dest = implode('.', $parts) . '.png'; } else { $dest = $src; } if ($fileExtension == "jpg" || $fileExtension == 'jpeg') { $newImage = ImageCreateFromJpeg($src); } elseif ($fileExtension == 'gif') { $newImage = imageCreateFromGIF($src); } imagepng($newImage, $dest); imagedestroy($newImage); if ($destroyOriginal) { unlink($src); } return $dest; }
/** * Image Resource ID for Watermark * * @var string * */ function ngg_Thumbnail($fileName, $no_ErrorImage = false) { //make sure the GD library is installed if (!function_exists("gd_info")) { echo 'You do not have the GD Library installed. This class requires the GD library to function properly.' . "\n"; echo 'visit http://us2.php.net/manual/en/ref.image.php for more information'; C_NextGEN_Bootstrap::shutdown(); } //initialize variables $this->errmsg = ''; $this->error = false; $this->currentDimensions = array(); $this->newDimensions = array(); $this->fileName = $fileName; $this->percent = 100; $this->maxWidth = 0; $this->maxHeight = 0; $this->watermarkImgPath = ''; $this->watermarkText = ''; //check to see if file exists if (!file_exists($this->fileName)) { $this->errmsg = 'File not found'; $this->error = true; } elseif (!is_readable($this->fileName)) { $this->errmsg = 'File is not readable'; $this->error = true; } //if there are no errors, determine the file format if ($this->error == false) { $data = @getimagesize($this->fileName); if (isset($data) && is_array($data)) { $extensions = array('1' => 'GIF', '2' => 'JPG', '3' => 'PNG'); $extension = array_key_exists($data[2], $extensions) ? $extensions[$data[2]] : ''; if ($extension) { $this->format = $extension; } else { $this->errmsg = 'Unknown file format'; $this->error = true; } } else { $this->errmsg = 'File is not an image'; $this->error = true; } } // increase memory-limit if possible, GD needs this for large images // @ini_set('memory_limit', '128M'); if ($this->error == false) { // Check memory consumption if file exists $this->checkMemoryForImage($this->fileName); } //initialize resources if no errors if ($this->error == false) { switch ($this->format) { case 'GIF': $this->oldImage = ImageCreateFromGif($this->fileName); break; case 'JPG': $this->oldImage = ImageCreateFromJpeg($this->fileName); break; case 'PNG': $this->oldImage = ImageCreateFromPng($this->fileName); break; } if (!$this->oldImage) { $this->errmsg = 'Create Image failed. Check memory limit'; $this->error = true; } else { $size = GetImageSize($this->fileName); $this->currentDimensions = array('width' => $size[0], 'height' => $size[1]); $this->newImage = $this->oldImage; } } if ($this->error == true) { if (!$no_ErrorImage) { $this->showErrorImage(); } return; } }
{ $m = intval($m); $pass = ""; for ($i = 0; $i < $m; $i++) { $te = mt_rand(48, 122); if ($te > 57 && $te < 65 || $te > 90 && $te < 97) { $te = $te - 9; } $pass .= chr($te); } return $pass; } switch (isset($_REQUEST['code'])) { case "gfx": $code = substr(hexdec(md5("" . date("F j") . "" . $_REQUEST["random_num"] . "" . $sitekey . "")), 2, 6); $image = ImageCreateFromJpeg("images/code_bg.jpg"); $text_color = ImageColorAllocate($image, 100, 100, 100); Header("Content-type: image/jpeg"); ImageString($image, 5, 12, 2, $code, $text_color); ImageJpeg($image, "", 50); ImageDestroy($image); exit; break; } // HTML and Word filter function text_filter($message, $type = "") { if (intval($type) == 2) { $message = htmlspecialchars(trim($message), ENT_QUOTES); } else { $message = strip_tags(urldecode($message));
/** * @since 2.0 */ public final function createImageFromFile() { if ($this->isJPEG()) { $this->image = ImageCreateFromJpeg($this->fullPath()); } else { if ($this->isGIF()) { $this->image = ImageCreateFromGif($this->fullPath()); } else { if ($this->isPNG()) { $this->image = ImageCreateFromPng($this->fullPath()); } } } }
function resizeImage($in_filename, $out_filename, $width) { $src_img = ImageCreateFromJpeg($in_filename); $old_x = ImageSX($src_img); $old_y = ImageSY($src_img); /* find the "desired height" of this thumbnail, relative to the desired width */ $desired_height = floor($old_y * ($width / $old_x)); $dst_img = ImageCreateTrueColor($width, $desired_height); ImageCopyResampled($dst_img, $src_img, 0, 0, 0, 0, $width, $desired_height, $old_x, $old_y); ImageJpeg($dst_img, $out_filename, 100); ImageDestroy($dst_img); ImageDestroy($src_img); }
/** * 指定サイズで画像を出力する. * * @param string $FromImgPath ファイル名までのパス * @param string $ToImgPath 出力先パス * @param integer $tmpMW 最大横幅 * @param integer $tmpMH 最大縦幅 * @param integer $newFileName 新ファイル名 * @param array 新ファイル名を格納した配列 */ public function MakeThumb($FromImgPath, $ToImgPath, $tmpMW, $tmpMH, $newFileName = '') { // 画像の最大横幅(単位:ピクセル) $ThmMaxWidth = LARGE_IMAGE_WIDTH; // 画像の最大縦幅(単位:ピクセル) $ThmMaxHeight = LARGE_IMAGE_HEIGHT; //拡張子取得 $array_ext = explode('.', $FromImgPath); $ext = $array_ext[count($array_ext) - 1]; $MW = $ThmMaxWidth; if ($tmpMW) { $MW = $tmpMW; } // $MWに最大横幅セット $MH = $ThmMaxHeight; if ($tmpMH) { $MH = $tmpMH; } // $MHに最大縦幅セット if (empty($FromImgPath) || empty($ToImgPath)) { return array(0, '出力元画像パス、または出力先フォルダが指定されていません。'); } if (!file_exists($FromImgPath)) { return array(0, '出力元画像が見つかりません。'); } $size = @GetImageSize($FromImgPath); $re_size = $size; // 画像の種類が不明 or swf if (!$size[2] || $size[2] > 3) { return array(0, '画像形式がサポートされていません。'); } //アスペクト比固定処理 $tmp_w = $size[0] / $MW; if ($MH != 0) { $tmp_h = $size[1] / $MH; } if ($tmp_w > 1 || $tmp_h > 1) { if ($MH == 0) { if ($tmp_w > 1) { $re_size[0] = $MW; $re_size[1] = $size[1] * $MW / $size[0]; } } else { if ($tmp_w > $tmp_h) { $re_size[0] = $MW; $re_size[1] = $size[1] * $MW / $size[0]; } else { $re_size[1] = $MH; $re_size[0] = $size[0] * $MH / $size[1]; } } } // サムネイル画像ファイル名作成処理 $tmp = array_pop(explode('/', $FromImgPath)); // /の一番最後を切り出し $FromFileName = array_shift(explode('.', $tmp)); // .で区切られた部分を切り出し $ToFile = $FromFileName; // 拡張子以外の部分までを作成 $ImgNew = imagecreatetruecolor($re_size[0], $re_size[1]); switch ($size[2]) { case '1': //gif形式 if ($tmp_w <= 1 && $tmp_h <= 1) { if ($newFileName) { $ToFile = $newFileName; } elseif ($ext) { $ToFile .= '.' . $ext; } else { $ToFile .= '.gif'; } if (!@copy($FromImgPath, $ToImgPath . $ToFile)) { // エラー処理 return array(0, 'ファイルのコピーに失敗しました。'); } ImageDestroy($ImgNew); return array(1, $ToFile); } ImageColorAllocate($ImgNew, 255, 235, 214); //背景色 $black = ImageColorAllocate($ImgNew, 0, 0, 0); $red = ImageColorAllocate($ImgNew, 255, 0, 0); Imagestring($ImgNew, 4, 5, 5, "GIF {$size['0']}x{$size['1']}", $red); ImageRectangle($ImgNew, 0, 0, $re_size[0] - 1, $re_size[1] - 1, $black); if ($newFileName) { $ToFile = $newFileName; } elseif ($ext) { $ToFile .= '.' . $ext; } else { $ToFile .= '.png'; } $TmpPath = $ToImgPath . $ToFile; @Imagepng($ImgNew, $TmpPath); // 画像が作成されていない場合 if (!@file_exists($TmpPath)) { return array(0, '画像の出力に失敗しました。'); } ImageDestroy($ImgNew); return array(1, $ToFile); case '2': //jpg形式 $ImgDefault = ImageCreateFromJpeg($FromImgPath); //ImageCopyResized($ImgNew, $ImgDefault, 0, 0, 0, 0, $re_size[0], $re_size[1], $size[0], $size[1]); if ($re_size[0] != $size[0] || $re_size[0] != $size[0]) { ImageCopyResampled($ImgNew, $ImgDefault, 0, 0, 0, 0, $re_size[0], $re_size[1], $size[0], $size[1]); } GcUtils::gfDebugLog($size); GcUtils::gfDebugLog($re_size); if ($newFileName) { $ToFile = $newFileName; } elseif ($ext) { $ToFile .= '.' . $ext; } else { $ToFile .= '.jpg'; } $TmpPath = $ToImgPath . $ToFile; @ImageJpeg($ImgNew, $TmpPath); // 画像が作成されていない場合 if (!@file_exists($TmpPath)) { return array(0, "画像の出力に失敗しました。<br>{$ImgNew}<br>{$TmpPath}"); } $RetVal = $ToFile; break; case '3': //png形式 $ImgDefault = ImageCreateFromPNG($FromImgPath); //ImageCopyResized($ImgNew, $ImgDefault, 0, 0, 0, 0, $re_size[0], $re_size[1], $size[0], $size[1]); ImageCopyResampled($ImgNew, $ImgDefault, 0, 0, 0, 0, $re_size[0], $re_size[1], $size[0], $size[1]); if ($newFileName) { $ToFile = $newFileName; } elseif ($ext) { $ToFile .= '.' . $ext; } else { $ToFile .= '.png'; } $TmpPath = $ToImgPath . $ToFile; @ImagePNG($ImgNew, $TmpPath); // 画像が作成されていない場合 if (!@file_exists($TmpPath)) { return array(0, '画像の出力に失敗しました。'); } $RetVal = $ToFile; break; } ImageDestroy($ImgDefault); ImageDestroy($ImgNew); return array(1, $RetVal); }
function generateImage($source_file, $cache_file, $resolution) { global $sharpen, $jpg_quality; $extension = strtolower(pathinfo($source_file, PATHINFO_EXTENSION)); // Check the image dimensions $dimensions = GetImageSize($source_file); $width = $dimensions[0]; $height = $dimensions[1]; // Do we need to downscale the image? if ($width <= $resolution) { // no, because the width of the source image is already less than the client width return $source_file; } // We need to resize the source image to the width of the resolution breakpoint we're working with $ratio = $height / $width; $new_width = $resolution; $new_height = ceil($new_width * $ratio); $dst = ImageCreateTrueColor($new_width, $new_height); // re-sized image switch ($extension) { case 'png': $src = @ImageCreateFromPng($source_file); // original image break; case 'gif': $src = @ImageCreateFromGif($source_file); // original image break; default: $src = @ImageCreateFromJpeg($source_file); // original image ImageInterlace($dst, true); // Enable interlancing (progressive JPG, smaller size file) break; } if ($extension == 'png') { imagealphablending($dst, false); imagesavealpha($dst, true); $transparent = imagecolorallocatealpha($dst, 255, 255, 255, 127); imagefilledrectangle($dst, 0, 0, $new_width, $new_height, $transparent); } ImageCopyResampled($dst, $src, 0, 0, 0, 0, $new_width, $new_height, $width, $height); // do the resize in memory ImageDestroy($src); // sharpen the image? // NOTE: requires PHP compiled with the bundled version of GD (see http://php.net/manual/en/function.imageconvolution.php) if ($sharpen == TRUE && function_exists('imageconvolution')) { $intSharpness = findSharp($width, $new_width); $arrMatrix = array(array(-1, -2, -1), array(-2, $intSharpness + 12, -2), array(-1, -2, -1)); imageconvolution($dst, $arrMatrix, $intSharpness, 0); } $cache_dir = dirname($cache_file); // does the directory exist already? if (!is_dir($cache_dir)) { if (!mkdir($cache_dir, 0755, true)) { // check again if it really doesn't exist to protect against race conditions if (!is_dir($cache_dir)) { // uh-oh, failed to make that directory ImageDestroy($dst); sendErrorImage("Failed to create cache directory: {$cache_dir}"); } } } if (!is_writable($cache_dir)) { sendErrorImage("The cache directory is not writable: {$cache_dir}"); } // save the new file in the appropriate path, and send a version to the browser switch ($extension) { case 'png': $gotSaved = ImagePng($dst, $cache_file); break; case 'gif': $gotSaved = ImageGif($dst, $cache_file); break; default: $gotSaved = ImageJpeg($dst, $cache_file, $jpg_quality); break; } ImageDestroy($dst); if (!$gotSaved && !file_exists($cache_file)) { sendErrorImage("Failed to create image: {$cache_file}"); } return $cache_file; }