function resizeImgH($imgname, $height) { $img_src = ImageCreateFromjpeg($imgname); $true_width = imagesx($img_src); $true_height = imagesy($img_src); $width = $height / $true_height * $true_width; $img_des = ImageCreateTruecolor($width, $height); imagecopyresampled($img_des, $img_src, 0, 0, 0, 0, $width, $height, $true_width, $true_height); return $img_des; }
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; } }
function imageZoom($srcFile, $dstW = '', $dstH = '', $url = '') { header("Content-type: image/jpeg"); $data = GetImageSize($srcFile, $info); switch ($data[2]) { case 1: $im = @ImageCreateFromGIF($srcFile); break; case 2: $im = @ImageCreateFromJPEG($srcFile); break; case 3: $im = @ImageCreateFromPNG($srcFile); break; } $srcW = ImageSX($im); $srcH = ImageSY($im); $newsize = $this->getZoomSize($srcW, $srcH, $dstW, $dstH); $dstW = $newsize[0]; $dstH = $newsize[1]; if (function_exists("ImageCreateTruecolor")) { $ni = ImageCreateTruecolor($dstW, $dstH); } else { $ni = ImageCreate($dstW, $dstH); } ImageCopyResized($ni, $im, 0, 0, 0, 0, $dstW, $dstH, $srcW, $srcH); if ($url == "") { imagejpeg($ni); } else { if (!imagejpeg($ni, $url)) { return new sfException(lang::get("Was error!")); } } ImageDestroy($ni); ImageDestroy($im); }
public static function rotateImage($thumbName, $size, $angle = 90, &$errorMsg) { $params = JComponentHelper::getParams('com_phocagallery'); $jfile_thumbs = $params->get('jfile_thumbs', 1); $jpeg_quality = $params->get('jpeg_quality', 85); $jpeg_quality = PhocaGalleryImage::getJpegQuality($jpeg_quality); // Try to change the size $memory = 8; $memoryLimitChanged = 0; $memory = (int) ini_get('memory_limit'); if ($memory == 0) { $memory = 8; } $fileIn = $thumbName->abs; $fileOut = $thumbName->abs; if ($fileIn !== '' && file_exists($fileIn)) { //array of width, height, IMAGETYPE, "height=x width=x" (string) list($w, $h, $type) = GetImageSize($fileIn); // we got the info from GetImageSize if ($w > 0 && $h > 0 && $type != '') { // Change the $w against $h because of rotating $src = array(0, 0, $w, $h); $dst = array(0, 0, $h, $w); } else { $errorMsg = 'ErrorWorHorType'; return false; } // Try to increase memory if ($memory < 50) { ini_set('memory_limit', '50M'); $memoryLimitChanged = 1; } switch ($type) { case IMAGETYPE_JPEG: if (!function_exists('ImageCreateFromJPEG')) { $errorMsg = 'ErrorNoJPGFunction'; return false; } //$image1 = ImageCreateFromJPEG($fileIn); try { $image1 = ImageCreateFromJPEG($fileIn); } catch (\Exception $exception) { $errorMsg = 'ErrorJPGFunction'; return false; } break; case IMAGETYPE_PNG: if (!function_exists('ImageCreateFromPNG')) { $errorMsg = 'ErrorNoPNGFunction'; return false; } //$image1 = ImageCreateFromPNG($fileIn); try { $image1 = ImageCreateFromPNG($fileIn); } catch (\Exception $exception) { $errorMsg = 'ErrorPNGFunction'; return false; } break; case IMAGETYPE_GIF: if (!function_exists('ImageCreateFromGIF')) { $errorMsg = 'ErrorNoGIFFunction'; return false; } //$image1 = ImageCreateFromGIF($fileIn); try { $image1 = ImageCreateFromGIF($fileIn); } catch (\Exception $exception) { $errorMsg = 'ErrorGIFFunction'; return false; } break; case IMAGETYPE_WBMP: if (!function_exists('ImageCreateFromWBMP')) { $errorMsg = 'ErrorNoWBMPFunction'; return false; } //$image1 = ImageCreateFromWBMP($fileIn); try { $image1 = ImageCreateFromWBMP($fileIn); } catch (\Exception $exception) { $errorMsg = 'ErrorWBMPFunction'; return false; } break; default: $errorMsg = 'ErrorNotSupportedImage'; return false; break; } if ($image1) { // Building image for ROTATING /* $image2 = @ImageCreateTruecolor($dst[2], $dst[3]); if (!$image2) { return 'ErrorNoImageCreateTruecolor'; }*/ /* if(!function_exists("imagerotate")) { $errorMsg = 'ErrorNoImageRotate'; return false; }*/ switch ($type) { case IMAGETYPE_PNG: // imagealphablending($image1, false); // imagesavealpha($image1, true); if (!function_exists("imagecolorallocate")) { $errorMsg = 'ErrorNoImageColorAllocate'; return false; } if (!function_exists("imagefill")) { $errorMsg = 'ErrorNoImageFill'; return false; } if (!function_exists("imagecolortransparent")) { $errorMsg = 'ErrorNoImageColorTransparent'; return false; } $colBlack = imagecolorallocate($image1, 0, 0, 0); if (!function_exists("imagerotate")) { $image2 = PhocaGalleryImageRotate::imageRotate($image1, $angle, $colBlack); } else { $image2 = imagerotate($image1, $angle, $colBlack); } imagefill($image2, 0, 0, $colBlack); imagecolortransparent($image2, $colBlack); break; default: if (!function_exists("imagerotate")) { $image2 = PhocaGalleryImageRotate::imageRotate($image1, $angle, 0); } else { $image2 = imageRotate($image1, $angle, 0); } break; } // Get the image size and resize the rotated image if necessary $rotateWidth = imagesx($image2); // Get the size from rotated image $rotateHeight = imagesy($image2); // Get the size from rotated image $parameterSize = PhocaGalleryFileThumbnail::getThumbnailResize($size); $newWidth = $parameterSize['width']; // Get maximum sizes, they can be displayed $newHeight = $parameterSize['height']; // Get maximum sizes, they can be displayed $scale = $newWidth / $rotateWidth < $newHeight / $rotateHeight ? $newWidth / $rotateWidth : $newHeight / $rotateHeight; // smaller rate $src = array(0, 0, $rotateWidth, $rotateHeight); $dst = array(0, 0, floor($rotateWidth * $scale), floor($rotateHeight * $scale)); // If original is smaller than thumbnail size, don't resize it if ($src[2] > $dst[2] || $src[3] > $dst[3]) { // Building image for RESIZING THE ROTATED IMAGE $image3 = @ImageCreateTruecolor($dst[2], $dst[3]); if (!$image3) { $errorMsg = 'ErrorNoImageCreateTruecolor'; return false; } ImageCopyResampled($image3, $image2, $dst[0], $dst[1], $src[0], $src[1], $dst[2], $dst[3], $src[2], $src[3]); switch ($type) { case IMAGETYPE_PNG: // imagealphablending($image2, true); // imagesavealpha($image2, true); if (!function_exists("imagecolorallocate")) { $errorMsg = 'ErrorNoImageColorAllocate'; return false; } if (!function_exists("imagefill")) { $errorMsg = 'ErrorNoImageFill'; return false; } if (!function_exists("imagecolortransparent")) { $errorMsg = 'ErrorNoImageColorTransparent'; return false; } $colBlack = imagecolorallocate($image3, 0, 0, 0); imagefill($image3, 0, 0, $colBlack); imagecolortransparent($image3, $colBlack); break; } } else { $image3 = $image2; } switch ($type) { case IMAGETYPE_JPEG: if (!function_exists('ImageJPEG')) { $errorMsg = 'ErrorNoJPGFunction'; return false; } if ($jfile_thumbs == 1) { ob_start(); if (!@ImageJPEG($image3, NULL, $jpeg_quality)) { ob_end_clean(); $errorMsg = 'ErrorWriteFile'; return false; } $imgJPEGToWrite = ob_get_contents(); ob_end_clean(); if (!JFile::write($fileOut, $imgJPEGToWrite)) { $errorMsg = 'ErrorWriteFile'; return false; } } else { if (!@ImageJPEG($image3, $fileOut, $jpeg_quality)) { $errorMsg = 'ErrorWriteFile'; return false; } } break; case IMAGETYPE_PNG: if (!function_exists('ImagePNG')) { $errorMsg = 'ErrorNoPNGFunction'; return false; } if ($jfile_thumbs == 1) { ob_start(); if (!@ImagePNG($image3, NULL)) { ob_end_clean(); $errorMsg = 'ErrorWriteFile'; return false; } $imgPNGToWrite = ob_get_contents(); ob_end_clean(); if (!JFile::write($fileOut, $imgPNGToWrite)) { $errorMsg = 'ErrorWriteFile'; return false; } } else { if (!@ImagePNG($image3, $fileOut)) { $errorMsg = 'ErrorWriteFile'; return false; } } break; case IMAGETYPE_GIF: if (!function_exists('ImageGIF')) { $errorMsg = 'ErrorNoGIFFunction'; return false; } if ($jfile_thumbs == 1) { ob_start(); if (!@ImageGIF($image3, NULL)) { ob_end_clean(); $errorMsg = 'ErrorWriteFile'; return false; } $imgGIFToWrite = ob_get_contents(); ob_end_clean(); if (!JFile::write($fileOut, $imgGIFToWrite)) { $errorMsg = 'ErrorWriteFile'; return false; } } else { if (!@ImageGIF($image3, $fileOut)) { $errorMsg = 'ErrorWriteFile'; return false; } } break; default: $errorMsg = 'ErrorNotSupportedImage'; return false; break; } // free memory ImageDestroy($image1); // Original ImageDestroy($image2); // Rotated ImageDestroy($image3); // Resized if ($memoryLimitChanged == 1) { $memoryString = $memory . 'M'; ini_set('memory_limit', $memoryString); } return true; // Success } else { $errorMsg = PhocaGalleryUtils::setMessage($errorMsg, JText::_('COM_PHOCAGALLERY_ERROR_IMAGE_NOT_PROCESS')); return false; } if ($memoryLimitChanged == 1) { $memoryString = $memory . 'M'; ini_set('memory_limit', $memoryString); } } $errorMsg = JText::_('COM_PHOCAGALLERY_FILEORIGINAL_NOT_EXISTS'); return false; }
function cut($newX, $newY, $srcX = 0, $srcY = 0) { if ($this->img) { $X = ImageSX($this->img); $Y = ImageSY($this->img); $newX = $this->_convert($newX, $X); $newY = $this->_convert($newY, $Y); if (!$newX) { $newX = $X; } if (!$newY) { $newY = $Y; } if (!($newimg = ImageCreateTruecolor($X, $Y))) { $newimg = ImageCreate($X, $Y); } ImageCopy($newimg, $this->img, 0, 0, 0, 0, $X, $Y); ImageDestroy($this->img); if (!($this->img = ImageCreateTruecolor($newX, $newY))) { $this->img = ImageCreate($newX, $newY); } imagecopy($this->img, $newimg, 0, 0, $srcX, $srcY, $newX, $newY); ImageDestroy($newimg); return true; } else { return false; } }
/** * Converts an image to another type * @param string $filetype JPG, PNG, GIF * @param array $bgcolor for converting PNG whith transparency to JPG * @return \ImageManipulationWithGd\Imwg */ public function convert($filetype = null, $bgcolor = array(255, 255, 255)) { switch ($filetype) { case "JPG": if (static::$file_info['type'] == "PNG") { // Fill png transparency with $bgcolor $tempImage = ImageCreateTruecolor(static::$file_info['width'], static::$file_info['height']); $col = imagecolorallocate($tempImage, $bgcolor[0], $bgcolor[1], $bgcolor[2]); imagefill($tempImage, 0, 0, $col); ImageCopyResampled($tempImage, static::$image_resource, 0, 0, 0, 0, static::$file_info['width'], static::$file_info['height'], static::$file_info['width'], static::$file_info['height']); ImageDestroy(static::$image_resource); static::$image_resource = $tempImage; static::$image_quality = 92; } static::$file_info['type'] = "JPG"; static::$file_info['mime'] = "image/jpg"; break; case "PNG": static::$file_info['type'] = "PNG"; static::$file_info['mime'] = "image/png"; static::$image_quality = 0; break; case "GIF": static::$file_info['type'] = "GIF"; static::$file_info['mime'] = "image/gif"; break; default: // nothing break; } return $this; }
/** * need GD library (first PHP line WIN: dl("php_gd.dll"); UNIX: dl("gd.so"); * www.boutell.com/gd/ * interval.cz/clanky/php-skript-pro-generovani-galerie-obrazku-2/ * cz.php.net/imagecopyresampled * www.linuxsoft.cz/sw_detail.php?id_item=871 * www.webtip.cz/art/wt_tech_php/liquid_ir.html * php.vrana.cz/zmensovani-obrazku.php * diskuse.jakpsatweb.cz/ * * @param string $fileIn Vstupni soubor (mel by existovat) * @param string $fileOut Vystupni soubor, null ho jenom zobrazi (taky kdyz nema pravo se zapsat :) * @param int $width Vysledna sirka (maximalni) * @param int $height Vysledna vyska (maximalni) * @param bool $crop Orez (true, obrazek bude presne tak velky), jinak jenom Resample (udane maximalni rozmery) * @param int $typeOut IMAGETYPE_type vystupniho obrazku * @return bool Chyba kdyz vrati false */ public static function imageMagic($fileIn, $fileOut = null, $width = null, $height = null, $crop = null, $typeOut = null, $watermarkParams = array(), $frontUpload = 0, &$errorMsg) { $params = JComponentHelper::getParams('com_phocagallery'); $jfile_thumbs = $params->get('jfile_thumbs', 1); $jpeg_quality = $params->get('jpeg_quality', 85); $jpeg_quality = PhocaGalleryImage::getJpegQuality($jpeg_quality); $fileWatermark = ''; // While front upload we don't display the process page if ($frontUpload == 0) { $stopText = PhocaGalleryRenderProcess::displayStopThumbnailsCreating('processpage'); echo $stopText; } // Memory - - - - - - - - $memory = 8; $memoryLimitChanged = 0; $memory = (int) ini_get('memory_limit'); if ($memory == 0) { $memory = 8; } // - - - - - - - - - - - if ($fileIn !== '' && JFile::exists($fileIn)) { // array of width, height, IMAGETYPE, "height=x width=x" (string) list($w, $h, $type) = GetImageSize($fileIn); if ($w > 0 && $h > 0) { // we got the info from GetImageSize // size of the image if ($width == null || $width == 0) { // no width added $width = $w; } else { if ($height == null || $height == 0) { // no height, adding the same as width $height = $width; } } if ($height == null || $height == 0) { // no height, no width $height = $h; } // miniaturizing if (!$crop) { // new size - nw, nh (new width/height) $scale = $width / $w < $height / $h ? $width / $w : $height / $h; // smaller rate $src = array(0, 0, $w, $h); $dst = array(0, 0, floor($w * $scale), floor($h * $scale)); } else { // will be cropped $scale = $width / $w > $height / $h ? $width / $w : $height / $h; // greater rate $newW = $width / $scale; // check the size of in file $newH = $height / $scale; // which side is larger (rounding error) if ($w - $newW > $h - $newH) { $src = array(floor(($w - $newW) / 2), 0, floor($newW), $h); } else { $src = array(0, floor(($h - $newH) / 2), $w, floor($newH)); } $dst = array(0, 0, floor($width), floor($height)); } // Watermark - - - - - - - - - - - if (!empty($watermarkParams) && ($watermarkParams['create'] == 1 || $watermarkParams['create'] == 2)) { $thumbnailSmall = false; $thumbnailMedium = false; $thumbnailLarge = false; $thumbnailMedium = preg_match("/phoca_thumb_m_/i", $fileOut); $thumbnailLarge = preg_match("/phoca_thumb_l_/i", $fileOut); $path = PhocaGalleryPath::getPath(); $fileName = PhocaGalleryFile::getTitleFromFile($fileIn, 1); // Which Watermark will be used // If watermark is in current directory use it else use Default $fileWatermarkMedium = str_replace($fileName, 'watermark-medium.png', $fileIn); $fileWatermarkLarge = str_replace($fileName, 'watermark-large.png', $fileIn); clearstatcache(); // Which Watermark will be used if ($thumbnailMedium) { if (JFile::exists($fileWatermarkMedium)) { $fileWatermark = $fileWatermarkMedium; } else { if ($watermarkParams['create'] == 2) { $fileWatermark = $path->image_abs . 'watermark-medium.png'; } else { $fileWatermark = ''; } } } else { if ($thumbnailLarge) { if (JFile::exists($fileWatermarkLarge)) { $fileWatermark = $fileWatermarkLarge; } else { if ($watermarkParams['create'] == 2) { $fileWatermark = $path->image_abs . 'watermark-large.png'; } else { $fileWatermark = ''; } } } else { $fileWatermark = ''; } } if (!JFile::exists($fileWatermark)) { $fileWatermark = ''; } if ($fileWatermark != '') { list($wW, $hW, $typeW) = GetImageSize($fileWatermark); switch ($watermarkParams['x']) { case 'left': $locationX = 0; break; case 'right': $locationX = $dst[2] - $wW; break; case 'center': default: $locationX = $dst[2] / 2 - $wW / 2; break; } switch ($watermarkParams['y']) { case 'top': $locationY = 0; break; case 'bottom': $locationY = $dst[3] - $hW; break; case 'middle': default: $locationY = $dst[3] / 2 - $hW / 2; break; } } } else { $fileWatermark = ''; } } if ($memory < 50) { ini_set('memory_limit', '50M'); $memoryLimitChanged = 1; } // Resampling // in file // Watemark if ($fileWatermark != '') { if (!function_exists('ImageCreateFromPNG')) { $errorMsg = 'ErrorNoPNGFunction'; return false; } $waterImage1 = ImageCreateFromPNG($fileWatermark); } // End Watermark - - - - - - - - - - - - - - - - - - switch ($type) { case IMAGETYPE_JPEG: if (!function_exists('ImageCreateFromJPEG')) { $errorMsg = 'ErrorNoJPGFunction'; return false; } //$image1 = ImageCreateFromJPEG($fileIn); try { $image1 = ImageCreateFromJPEG($fileIn); } catch (\Exception $exception) { $errorMsg = 'ErrorJPGFunction'; return false; } break; case IMAGETYPE_PNG: if (!function_exists('ImageCreateFromPNG')) { $errorMsg = 'ErrorNoPNGFunction'; return false; } //$image1 = ImageCreateFromPNG($fileIn); try { $image1 = ImageCreateFromPNG($fileIn); } catch (\Exception $exception) { $errorMsg = 'ErrorPNGFunction'; return false; } break; case IMAGETYPE_GIF: if (!function_exists('ImageCreateFromGIF')) { $errorMsg = 'ErrorNoGIFFunction'; return false; } //$image1 = ImageCreateFromGIF($fileIn); try { $image1 = ImageCreateFromGIF($fileIn); } catch (\Exception $exception) { $errorMsg = 'ErrorGIFFunction'; return false; } break; case IMAGETYPE_WBMP: if (!function_exists('ImageCreateFromWBMP')) { $errorMsg = 'ErrorNoWBMPFunction'; return false; } //$image1 = ImageCreateFromWBMP($fileIn); try { $image1 = ImageCreateFromWBMP($fileIn); } catch (\Exception $exception) { $errorMsg = 'ErrorWBMPFunction'; return false; } break; default: $errorMsg = 'ErrorNotSupportedImage'; return false; break; } if ($image1) { $image2 = @ImageCreateTruecolor($dst[2], $dst[3]); if (!$image2) { $errorMsg = 'ErrorNoImageCreateTruecolor'; return false; } switch ($type) { case IMAGETYPE_PNG: //imagealphablending($image1, false); @imagealphablending($image2, false); //imagesavealpha($image1, true); @imagesavealpha($image2, true); break; } ImageCopyResampled($image2, $image1, $dst[0], $dst[1], $src[0], $src[1], $dst[2], $dst[3], $src[2], $src[3]); // Watermark - - - - - - if ($fileWatermark != '') { ImageCopy($image2, $waterImage1, $locationX, $locationY, 0, 0, $wW, $hW); } // End Watermark - - - - // Display the Image - not used if ($fileOut == null) { header("Content-type: " . image_type_to_mime_type($typeOut)); } // Create the file if ($typeOut == null) { // no bitmap $typeOut = $type == IMAGETYPE_WBMP ? IMAGETYPE_PNG : $type; } switch ($typeOut) { case IMAGETYPE_JPEG: if (!function_exists('ImageJPEG')) { $errorMsg = 'ErrorNoJPGFunction'; return false; } if ($jfile_thumbs == 1) { ob_start(); if (!@ImageJPEG($image2, NULL, $jpeg_quality)) { ob_end_clean(); $errorMsg = 'ErrorWriteFile'; return false; } $imgJPEGToWrite = ob_get_contents(); ob_end_clean(); if (!JFile::write($fileOut, $imgJPEGToWrite)) { $errorMsg = 'ErrorWriteFile'; return false; } } else { if (!@ImageJPEG($image2, $fileOut, $jpeg_quality)) { $errorMsg = 'ErrorWriteFile'; return false; } } break; case IMAGETYPE_PNG: if (!function_exists('ImagePNG')) { $errorMsg = 'ErrorNoPNGFunction'; return false; } if ($jfile_thumbs == 1) { ob_start(); if (!@ImagePNG($image2, NULL)) { ob_end_clean(); $errorMsg = 'ErrorWriteFile'; return false; } $imgPNGToWrite = ob_get_contents(); ob_end_clean(); if (!JFile::write($fileOut, $imgPNGToWrite)) { $errorMsg = 'ErrorWriteFile'; return false; } } else { if (!@ImagePNG($image2, $fileOut)) { $errorMsg = 'ErrorWriteFile'; return false; } } break; case IMAGETYPE_GIF: if (!function_exists('ImageGIF')) { $errorMsg = 'ErrorNoGIFFunction'; return false; } if ($jfile_thumbs == 1) { ob_start(); if (!@ImageGIF($image2, NULL)) { ob_end_clean(); $errorMsg = 'ErrorWriteFile'; return false; } $imgGIFToWrite = ob_get_contents(); ob_end_clean(); if (!JFile::write($fileOut, $imgGIFToWrite)) { $errorMsg = 'ErrorWriteFile'; return false; } } else { if (!@ImageGIF($image2, $fileOut)) { $errorMsg = 'ErrorWriteFile'; return false; } } break; default: $errorMsg = 'ErrorNotSupportedImage'; return false; break; } // free memory ImageDestroy($image1); ImageDestroy($image2); if (isset($waterImage1)) { ImageDestroy($waterImage1); } if ($memoryLimitChanged == 1) { $memoryString = $memory . 'M'; ini_set('memory_limit', $memoryString); } $errorMsg = ''; // Success return true; } else { $errorMsg = 'Error1'; return false; } if ($memoryLimitChanged == 1) { $memoryString = $memory . 'M'; ini_set('memory_limit', $memoryString); } } $errorMsg = 'Error2'; return false; }
<?php $im = ImageCreateTruecolor(400, 300); ImageFilledRectangle($im, 0, 0, 399, 299, 0xffffff); ImageFilledEllipse($im, 200, 150, 300, 300, 0x0); ImageAlphaBlending($im, true); ImageFilledRectangle($im, 100, 0, 400, 100, 0x60ff1111); ImageFilledRectangle($im, 100, 100, 400, 200, 0x30ff1111); ImageFilledRectangle($im, 100, 200, 400, 300, 0x10ff1111); Header('Content-Type: image/png'); ImagePNG($im);
function create_captcha($data = '', $img_path = '', $img_url = '', $font_path = '') { /** * Function to create a random color * Note: We aren't using this outside this function so we will sit it inside * @auteur mastercode.nl * @param $type string Mode for the color * @return int **/ function color($type) { switch ($type) { case "bg": $color = rand(224, 255); break; case "text": $color = rand(0, 127); break; case "grid": $color = rand(200, 224); break; default: $color = rand(0, 255); break; } return $color; } $defaults = array('word' => '', 'img_path' => '', 'img_url' => '', 'img_width' => '150', 'img_height' => '30', 'font_size' => '', 'font_path' => '', 'show_grid' => true, 'skew' => true, 'expiration' => 7200); foreach ($defaults as $key => $val) { if (!is_array($data)) { if (!isset(${$key}) or ${$key} == '') { ${$key} = $val; } } else { ${$key} = !isset($data[$key]) ? $val : $data[$key]; } } if ($img_path == '' or $img_url == '') { return FALSE; } if (!@is_dir($img_path)) { return FALSE; } if (!is_really_writable($img_path)) { return FALSE; } if (!extension_loaded('gd')) { return FALSE; } // ----------------------------------- // Select random Font from folder // ----------------------------------- if (is_dir($font_path)) { $handle = opendir($font_path); while (($file = @readdir($handle)) !== false) { if (!in_array($file, array('.', '..')) && substr($file, strlen($file) - 4, 4) == '.ttf') { $fonts[] = $file; } } $font_file = $font_path . DIRECTORY_SEPARATOR . $fonts[array_rand($fonts)]; } else { $font_file = $font_path; } // ----------------------------------- // Remove old images // ----------------------------------- list($usec, $sec) = explode(" ", microtime()); $now = (double) $usec + (double) $sec; $current_dir = @opendir($img_path); while ($filename = @readdir($current_dir)) { if ($filename != "." and $filename != ".." and $filename != "index.html") { $name = str_replace(".png", "", $filename); if ($name + $expiration < $now) { @unlink($img_path . $filename); } } } @closedir($current_dir); // ----------------------------------- // Do we have a "word" yet? // ----------------------------------- if ($word == '') { // No Zero (for user clarity); $pool = '123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'; $str = ''; for ($i = 0; $i < 6; $i++) { $str .= substr($pool, mt_rand(0, strlen($pool) - 1), 1); } $word = strtoupper($str); } // ----------------------------------- // Length of Word // ----------------------------------- $length = strlen($word); // ----------------------------------- // Create image // ----------------------------------- $im = ImageCreateTruecolor($img_width, $img_height); // ----------------------------------- // Assign colors // ----------------------------------- $bg_color = imagecolorallocate($im, color('bg'), color('bg'), color('bg')); $border_color = imagecolorallocate($im, 153, 102, 102); $text_color = imagecolorallocate($im, color('text'), color('text'), color('text')); $grid_color[] = imagecolorallocate($im, color('grid'), color('grid'), color('grid')); $grid_color[] = $grid_color[0] + 150; $grid_color[] = $grid_color[0] + 180; $grid_color[] = $grid_color[0] + 210; $shadow_color = imagecolorallocate($im, 255, 240, 240); // ----------------------------------- // Create the rectangle // ----------------------------------- ImageFilledRectangle($im, 0, 0, $img_width, $img_height, $bg_color); if ($show_grid == TRUE) { // X grid $grid = rand(20, 25); for ($x = 0; $x < $img_width; $x += mt_rand($grid - 2, $grid + 2)) { $current_colour = $grid_color[array_rand($grid_color)]; imagedashedline($im, mt_rand($x - 3, $x + 3), mt_rand(0, 4), mt_rand($x - 3, $x + 3), mt_rand($img_height - 5, $img_height), $current_colour); } // Y grid for ($y = 0; $y < $img_height; $y += mt_rand($grid - 2, $grid + 2)) { $current_colour = $grid_color[array_rand($grid_color)]; imageline($im, mt_rand(0, 4), mt_rand($y - 3, $y), mt_rand($img_width - 5, $img_width), mt_rand($y - 3, $y), $current_colour); } } // ----------------------------------- // Write the text // ----------------------------------- $use_font = ($font_file != '' and file_exists($font_file) and function_exists('imagettftext')) ? TRUE : FALSE; if ($use_font == FALSE) { $font_size = 5; $x = rand(2, $img_width / ($length / 3)); // y isnt used here } else { // Make font proportional to the image size $font_size = !empty($font_size) ? $font_size : mt_rand(18, 25); $x = rand(4, $img_width - ($font_size + ($font_size >> 1)) * $length); // y isnt used here } for ($i = 0; $i < strlen($word); $i++) { if ($use_font == FALSE) { $y = rand(0, $img_height / 2); imagestring($im, $font_size, $x, $y, substr($word, $i, 1), $text_color); $x += $font_size * 2; } else { $letter = substr($word, $i, 1); $less_rotate = array('c', 'N', 'U', 'Z', '7', '6', '9'); //letters that we don't want rotated too much... $angle = $skew == TRUE ? in_array($letter, $less_rotate) ? rand(-5, 5) : rand(-15, 15) : 0; $y = $img_height / 2 + ($font_size >> 1) + ($skew == TRUE ? rand(-9, 9) : 0); $x += $font_size >> 2; imagettftext($im, $font_size, $angle, $x, $y, $text_color, $font_file, $letter); $x += $font_size + ($font_size >> 2); } } // ----------------------------------- // Create the border // ----------------------------------- imagerectangle($im, 0, 0, $img_width - 1, $img_height - 1, $border_color); // ----------------------------------- // Generate the image // ----------------------------------- $img_name = $now . '.png'; ImagePNG($im, $img_path . $img_name); $img = "<img src=\"{$img_url}{$img_name}\" width=\"{$img_width}\" height=\"{$img_height}\" style=\"border:0;\" alt=\" \" />"; ImageDestroy($im); return array('word' => $word, 'time' => $now, 'image' => $img); }
function resize_on_the_fly($data, $width = 160) { $image = imagecreatefromstring($data); $true_width = imagesx($image); $true_height = imagesy($image); $height = $true_height * $width / $true_width; $img_des = ImageCreateTruecolor($width, $height); imagecopyresampled($img_des, $image, 0, 0, 0, 0, $width, $height, $true_width, $true_height); debug(__FILE__ . "**** IMAGE RESAMPLING ****: {$width}, {$height}", "green"); return imagejpeg($img_des); }
function make_transparent() { $info = GetImageSize($this->source_image); $width = $info[0]; $height = $info[1]; $mime = $info['mime']; // What sort of image? $type = substr(strrchr($mime, '/'), 1); switch ($type) { case 'jpeg': $image_create_func = 'ImageCreateFromJPEG'; $image_save_func = 'ImageJPEG'; $new_image_ext = 'jpg'; break; case 'png': $image_create_func = 'ImageCreateFromPNG'; $image_save_func = 'ImagePNG'; $new_image_ext = 'png'; break; case 'bmp': $image_create_func = 'ImageCreateFromBMP'; $image_save_func = 'ImageBMP'; $new_image_ext = 'bmp'; break; case 'gif': $image_create_func = 'ImageCreateFromGIF'; $image_save_func = 'ImageGIF'; $new_image_ext = 'gif'; break; case 'vnd.wap.wbmp': $image_create_func = 'ImageCreateFromWBMP'; $image_save_func = 'ImageWBMP'; $new_image_ext = 'bmp'; break; case 'xbm': $image_create_func = 'ImageCreateFromXBM'; $image_save_func = 'ImageXBM'; $new_image_ext = 'xbm'; break; default: $image_create_func = 'ImageCreateFromJPEG'; $image_save_func = 'ImageJPEG'; $new_image_ext = 'jpg'; } // Source Image $image = $image_create_func($this->source_image); $new_image = ImageCreateTruecolor($width, $height); // Set a White & Transparent Background Color $bg = ImageColorAllocateAlpha($new_image, 255, 255, 255, 127); // (PHP 4 >= 4.3.2, PHP 5) ImageFill($new_image, 0, 0, $bg); // Copy and merge ImageCopyMerge($new_image, $image, 0, 0, 0, 0, $width, $height, $this->pct); if ($this->save_to_folder) { if ($this->new_image_name) { $new_name = $this->new_image_name . '.' . $new_image_ext; } else { $new_name = $this->new_image_name(basename($this->source_image)) . '_transparent' . '.' . $new_image_ext; } $save_path = $this->save_to_folder . $new_name; } else { /* Show the image without saving it to a folder */ header("Content-Type: " . $mime); $image_save_func($new_image); $save_path = ''; } // Save image $process = $image_save_func($new_image, $save_path) or die("There was a problem in saving the new file."); return array('result' => $process, 'new_file_path' => $save_path); }
function makeImageTransparent($InFile, $OutFile, $tranparentPercent = 50) { $info = GetImageSize($InFile); $width = $info[0]; $height = $info[1]; $mime = $info['mime']; // What sort of image? $type = substr(strrchr($mime, '/'), 1); switch ($type) { case 'jpeg': $image_create_func = 'ImageCreateFromJPEG'; $image_save_func = 'ImageJPEG'; $new_image_ext = 'jpg'; break; case 'png': $image_create_func = 'ImageCreateFromPNG'; $image_save_func = 'ImagePNG'; $new_image_ext = 'png'; break; case 'bmp': $image_create_func = 'ImageCreateFromBMP'; $image_save_func = 'ImageBMP'; $new_image_ext = 'bmp'; break; case 'gif': $image_create_func = 'ImageCreateFromGIF'; $image_save_func = 'ImageGIF'; $new_image_ext = 'gif'; break; case 'vnd.wap.wbmp': $image_create_func = 'ImageCreateFromWBMP'; $image_save_func = 'ImageWBMP'; $new_image_ext = 'bmp'; break; case 'xbm': $image_create_func = 'ImageCreateFromXBM'; $image_save_func = 'ImageXBM'; $new_image_ext = 'xbm'; break; default: $image_create_func = 'ImageCreateFromJPEG'; $image_save_func = 'ImageJPEG'; $new_image_ext = 'jpg'; } // Source Image $image = $image_create_func($InFile); $new_image = ImageCreateTruecolor($width, $height); // Set a White & Transparent Background Color $bg = ImageColorAllocateAlpha($new_image, 255, 255, 255, 127); // (PHP 4 >= 4.3.2, PHP 5) ImageFill($new_image, 0, 0, $bg); // Copy and merge ImageCopyMerge($new_image, $image, 0, 0, 0, 0, $width, $height, $tranparentPercent); // Save image $image_save_func($new_image, $OutFile) or die("There was a problem in saving the new file."); imagedestroy($new_image); }
<?php $im = ImageCreateTruecolor(720, 180); ImageAlphaBlending($im, $blend); ImageFilledRectangle($im, 0, 0, 719, 179, 0xffffff); ImageTTFText($im, 80, 0, 10, 135, 0x0, "Garabd", "ABCdef123"); Header('Content-Type: image/png'); ImagePNG($im);
} for ($y = 0; $y < $height; $y++) { for ($x = $width - 1; $x > $right; $x--) { $rgb = imagecolorat($im, $x, $y); $colors = imagecolorsforindex($im, $rgb); if (($colors['red'] < 224 || $colors['green'] < 224 || $colors['blue'] < 224) && $colors['alpha'] < 64) { if ($x > $right) { $right = $x; break; } } } } $nheight = $bottom - $top + 2; $nwidth = $right - $left + 2; $dest = ImageCreateTruecolor($nwidth, $nheight); ImageAlphaBlending($dest, false); //这里很重要,意思是不合并颜色,直接用$img图像颜色替换,包括透明色; ImageSaveAlpha($dest, true); //这里很重要,意思是不要丢了$thumb图像的透明色; //$white = ImageColorAllocate($dest, 255, 255, 255); //ImageFill($dest, 0, 0, $white); //ImageColorTransparent($dest, $white); ImageCopy($dest, $im, 0, 0, $left - 1, $top - 1, $nwidth, $nheight); ImageDestroy($im); } if ($_GET["margin"]) { $margin = $_GET["margin"]; } elseif ($_GET["nocrop"]) { $margin = 0; } else {
public function execute($inputImagePath) { // 前処理 $filestat = @stat($inputImagePath); $imagesize = getimagesize($inputImagePath); $inputImageWidth = $imagesize[0]; $inputImageHeight = $imagesize[1]; $inputImageType = $imagesize[2]; // 今回実行用の一時変数をセット $output_image_width = $this->outputImageWidth; $output_image_height = is_null($this->outputImageHeight) ? $inputImageHeight * ($output_image_width / $inputImageWidth) : $this->outputImageHeight; // GIF 画像は縮小後も GIF 画像で出力する。旧機種対応などで、機種用の画像形式に変換したい場合、expr3 に固定する。 $output_image_type = $inputImageType == IMAGETYPE_GIF ? 'gif' : $this->outputImageType; $outputImageName = sha1($inputImagePath . '_' . $this->outputImageWidth . '_' . $this->outputFileSize . '_' . $filestat['mtime']) . '.' . $output_image_type; $outputImagePath = $this->outputImageDir . '/' . $outputImageName; if ($inputImageWidth <= $output_image_width) { if ($inputImageHeight <= $output_image_height) { $output_image_width = $inputImageWidth; $output_image_height = $inputImageHeight; } else { $output_image_width = $inputImageWidth * ($output_image_height / $inputImageHeight); } } else { if ($inputImageHeight <= $output_image_height) { $output_image_height = $inputImageHeight * ($output_image_width / $inputImageWidth); } else { if ($output_image_width / $inputImageWidth < $output_image_height / $inputImageHeight) { $output_image_height = $inputImageHeight * ($output_image_width / $inputImageWidth); } else { $output_image_width = $inputImageWidth * ($output_image_height / $inputImageHeight); } } } // ファイルが存在するか確認し、存在しない場合のみ作成する if (file_exists($outputImagePath)) { $info['convert'] = FALSE; } else { // 元ファイル作成 switch ($inputImageType) { case IMAGETYPE_GIF: $tempImage = imagecreatefromgif($inputImagePath); $arrTransparentColor = $this->getTransparentColor($tempImage); if (!empty($arrTransparentColor)) { imagecolortransparent($tempImage, $arrTransparentColor); } break; case IMAGETYPE_JPEG: $tempImage = imagecreatefromjpeg($inputImagePath); break; case IMAGETYPE_PNG: $tempImage = imagecreatefrompng($inputImagePath); break; case IMAGETYPE_WBMP: $tempImage = imagecreatefromwbmp($inputImagePath); break; } if (!$tempImage) { return false; } $scale = 1.0; $outputImagePathTemp = $outputImagePath . '.tmp-' . rand(); do { // 空ファイル作成 if ($output_image_type == 'gif') { // 縮小時のノイズ防止のためインデックスカラーで処理する。特に透過色を扱う上で重要。 $outputImage = ImageCreate($output_image_width * $scale, $output_image_height * $scale); } else { $outputImage = ImageCreateTruecolor($output_image_width * $scale, $output_image_height * $scale); } ImageCopyResampled($outputImage, $tempImage, 0, 0, 0, 0, $output_image_width * $scale, $output_image_height * $scale, $inputImageWidth, $inputImageHeight); // ファイル出力 @unlink($outputImagePathTemp); switch ($output_image_type) { case 'gif': if (!empty($arrTransparentColor)) { $transparent_color_id = imagecolorexact($outputImage, $arrTransparentColor['red'], $arrTransparentColor['green'], $arrTransparentColor['blue']); imagecolortransparent($outputImage, $transparent_color_id); } imagegif($outputImage, $outputImagePathTemp); break; case 'jpg': $quality = 75; // 表示可能なファイルサイズ以下になるまで、10%ずつクオリティを調整する do { @unlink($outputImagePathTemp); imagejpeg($outputImage, $outputImagePathTemp, $quality); $quality -= 10; clearstatcache(); } while (filesize($outputImagePathTemp) > $this->outputFileSize && $quality > 0); break; case 'png': imagepng($outputImage, $outputImagePathTemp); break; case 'bmp': imagewbmp($outputImage, $outputImagePathTemp); break; default: GC_Utils_Ex::gfPrintLog('不正な画像タイプ: '); break; } // メモリ開放 imagedestroy($outputImage); $scale -= 0.1; clearstatcache(); } while (filesize($outputImagePathTemp) > $this->outputFileSize && $scale >= 0.5); rename($outputImagePathTemp, $outputImagePath); // メモリ開放 imagedestroy($tempImage); $info['convert'] = TRUE; } $info['outputImagePath'] = $outputImagePath; $info['outputImageName'] = $outputImageName; return $info; }
/** * need GD library (first PHP line WIN: dl("php_gd.dll"); UNIX: dl("gd.so"); * www.boutell.com/gd/ * interval.cz/clanky/php-skript-pro-generovani-galerie-obrazku-2/ * cz.php.net/imagecopyresampled * www.linuxsoft.cz/sw_detail.php?id_item=871 * www.webtip.cz/art/wt_tech_php/liquid_ir.html * php.vrana.cz/zmensovani-obrazku.php * diskuse.jakpsatweb.cz/ * * @param string $file_in Vstupni soubor (mel by existovat) * @param string $file_out Vystupni soubor, null ho jenom zobrazi (taky kdyz nema pravo se zapsat :) * @param int $width Vysledna sirka (maximalni) * @param int $height Vysledna vyska (maximalni) * @param bool $crop Orez (true, obrazek bude presne tak velky), jinak jenom Resample (udane maximalni rozmery) * @param int $type_out IMAGETYPE_type vystupniho obrazku * @return bool Chyba kdyz vrati false */ function imageMagic($file_in, $file_out = null, $width = null, $height = null, $crop = null, $type_out = null, $watermarkParams = array(), $frontUpload = 0) { $fileWatermark = ''; // While front upload we don't display the process page if ($frontUpload == 0) { $stopText = PhocaGalleryHelper::displayStopThumbnailsCreating(); echo $stopText; } $memory = 8; $memoryLimitChanged = 0; $memory = (int) ini_get('memory_limit'); if ($memory == 0) { $memory = 8; } if ($file_in !== '' && file_exists($file_in)) { //array of width, height, IMAGETYPE, "height=x width=x" (string) list($w, $h, $type) = GetImageSize($file_in); if ($w > 0 && $h > 0) { // we got the info from GetImageSize // size of the image if ($width == null || $width == 0) { // no width added $width = $w; } else { if ($height == null || $height == 0) { // no height, adding the same as width $height = $width; } } if ($height == null || $height == 0) { // no height, no width $height = $h; } // miniaturizing if (!$crop) { // new size - nw, nh (new width/height) $scale = $width / $w < $height / $h ? $width / $w : $height / $h; // smaller rate $src = array(0, 0, $w, $h); $dst = array(0, 0, floor($w * $scale), floor($h * $scale)); } else { // will be cropped $scale = $width / $w > $height / $h ? $width / $w : $height / $h; // greater rate $newW = $width / $scale; // check the size of in file $newH = $height / $scale; // which side is larger (rounding error) if ($w - $newW > $h - $newH) { $src = array(floor(($w - $newW) / 2), 0, floor($newW), $h); } else { $src = array(0, floor(($h - $newH) / 2), $w, floor($newH)); } $dst = array(0, 0, floor($width), floor($height)); } // Watermark if (!empty($watermarkParams) && $watermarkParams['create'] == 1) { $thumbnailSmall = false; $thumbnailMedium = false; $thumbnailLarge = false; $thumbnailMedium = preg_match("/phoca_thumb_m_/i", $file_out); $thumbnailLarge = preg_match("/phoca_thumb_l_/i", $file_out); $fileName = PhocaGalleryHelper::getTitleFromFilenameWithExt($file_in); // Which Watermark will be used if ($thumbnailMedium) { $fileWatermark = str_replace($fileName, 'watermark-medium.png', $file_in); } else { if ($thumbnailLarge) { $fileWatermark = str_replace($fileName, 'watermark-large.png', $file_in); } else { $fileWatermark = ''; } } if (!file_exists($fileWatermark)) { $fileWatermark = ''; } if ($fileWatermark != '') { list($wW, $hW, $typeW) = GetImageSize($fileWatermark); switch ($watermarkParams['x']) { case 'left': $locationX = 0; break; case 'right': $locationX = $dst[2] - $wW; break; case 'center': default: $locationX = $dst[2] / 2 - $wW / 2; break; } switch ($watermarkParams['y']) { case 'top': $locationY = 0; break; case 'bottom': $locationY = $dst[3] - $hW; break; case 'middle': default: $locationY = $dst[3] / 2 - $hW / 2; break; } } } else { $fileWatermark = ''; } } if ($memory < 50) { ini_set('memory_limit', '50M'); $memoryLimitChanged = 1; } // Resampling // in file // Watemark if ($fileWatermark != '') { if (!function_exists('ImageCreateFromPNG')) { return 'ErrorNoPNGFunction'; } $waterImage1 = ImageCreateFromPNG($fileWatermark); } // End Watermark switch ($type) { case IMAGETYPE_JPEG: if (!function_exists('ImageCreateFromJPEG')) { return 'ErrorNoJPGFunction'; } $image1 = ImageCreateFromJPEG($file_in); break; case IMAGETYPE_PNG: if (!function_exists('ImageCreateFromPNG')) { return 'ErrorNoPNGFunction'; } $image1 = ImageCreateFromPNG($file_in); break; case IMAGETYPE_GIF: if (!function_exists('ImageCreateFromGIF')) { return 'ErrorNoGIFFunction'; } $image1 = ImageCreateFromGIF($file_in); break; case IMAGETYPE_WBMP: if (!function_exists('ImageCreateFromWBMP')) { return 'ErrorNoWBMPFunction'; } $image1 = ImageCreateFromWBMP($file_in); break; default: return 'ErrorNotSupportedImage'; break; } if ($image1) { // protection against invalid image dimensions /*foreach ($dst as $kdst =>$vdst) { if ($dst[$kdst] == 0 ) { $dst[$kdst] = 1; } } */ $image2 = @ImageCreateTruecolor($dst[2], $dst[3]); if (!$image2) { return 'ErrorNoImageCreateTruecolor'; } switch ($type) { case IMAGETYPE_PNG: //imagealphablending($image1, false); @imagealphablending($image2, false); //imagesavealpha($image1, true); @imagesavealpha($image2, true); break; } ImageCopyResampled($image2, $image1, $dst[0], $dst[1], $src[0], $src[1], $dst[2], $dst[3], $src[2], $src[3]); // Watermark if ($fileWatermark != '') { ImageCopy($image2, $waterImage1, $locationX, $locationY, 0, 0, $wW, $hW); } // End Watermark // display the image if ($file_out == null) { header("Content-type: " . image_type_to_mime_type($type_out)); } // out file if ($type_out == null) { // no bitmap $type_out = $type == IMAGETYPE_WBMP ? IMAGETYPE_PNG : $type; } switch ($type_out) { case IMAGETYPE_JPEG: if (!function_exists('ImageJPEG')) { return 'ErrorNoJPGFunction'; } if (!@ImageJPEG($image2, $file_out, 85)) { return 'ErrorWriteFile'; } break; case IMAGETYPE_PNG: if (!function_exists('ImagePNG')) { return 'ErrorNoPNGFunction'; } if (!@ImagePNG($image2, $file_out)) { return 'ErrorWriteFile'; } break; case IMAGETYPE_GIF: if (!function_exists('ImageGIF')) { return 'ErrorNoGIFFunction'; } if (!@ImageGIF($image2, $file_out)) { return 'ErrorWriteFile'; } break; default: return 'ErrorNotSupportedImage'; break; } // free memory ImageDestroy($image1); ImageDestroy($image2); if (isset($waterImage1)) { ImageDestroy($waterImage1); } if ($memoryLimitChanged == 1) { $memoryString = $memory . 'M'; ini_set('memory_limit', $memoryString); } return 'Success'; // Success } else { return 'Error1'; } if ($memoryLimitChanged == 1) { $memoryString = $memory . 'M'; ini_set('memory_limit', $memoryString); } } return 'Error2'; }
function display($tpl = null) { $document =& JFactory::getDocument(); $image_info = GetImageSize($this->filename); $memoryLimitChanged = 0; $memory = (int) ini_get('memory_limit'); $memory = $memory == 0 ? 8 : $memory; if ($memory < 50) { ini_set('memory_limit', '64M'); $memoryLimitChanged = 1; } // Content type $document->setMimeEncoding((string) $image_info['mime']); $this->assign('imgtype', $image_info[2]); $ratio_cropped = $this->w / $this->h; if ($this->maxwidth / $this->maxheight > $ratio_cropped) { $this->maxwidth = $this->maxheight * $ratio_cropped; } else { $this->maxheight = $this->maxwidth / $ratio_cropped; } /* echo '<pre>'.print_r($image_info, true).'</pre>'; echo '<br />From file: '.$this->filename; echo '<br />Horiz Offest(x): '.$this->x; echo '<br />Vert Offest(y): '.$this->y; echo '<br />Maxwidth: '.$this->maxwidth; echo '<br />Maxheight: '.$this->maxheight; , die;*/ switch ($this->imgtype) { case IMAGETYPE_JPEG: // Load $src = @ImageCreateFromJPEG($this->filename); $img = @ImageCreateTruecolor($this->maxwidth, $this->maxheight); // Resize cropped imagecopyresampled($img, $src, 0, 0, $this->x, $this->y, $this->maxwidth, $this->maxheight, $this->w, $this->h); // Output @ImageJPEG($img, null, 100); break; case IMAGETYPE_GIF: $src = @ImageCreateFromGIF($this->filename); $transp_index = imagecolortransparent($src); $img = @ImageCreateTruecolor($this->maxwidth, $this->maxheight); if ($transp_index >= 0) { // Get the original image's transparent color's RGB values $transp_color = imagecolorsforindex($src, $transp_index); // Allocate the same color in the new image resource $trans_index = imagecolorallocate($img, $transp_color['red'], $transp_color['green'], $transp_color['blue']); // Completely fill the background of the new image with allocated color. imagefill($img, 0, 0, $transp_index); // Set the background color for new image to transparent imagecolortransparent($img, $transp_index); } imagecopyresampled($img, $src, 0, 0, $this->x, $this->y, $this->maxwidth, $this->maxheight, $this->w, $this->h); @ImageGIF($img, null, 100); break; case IMAGETYPE_PNG: $src = @ImageCreateFromPNG($this->filename); $transp_index = imagecolortransparent($src); $img = @ImageCreateTruecolor($this->maxwidth, $this->maxheight); // Turn off transparency blending (temporarily) imagealphablending($img, false); // Create a new transparent color for image $color = imagecolorallocatealpha($img, 0, 0, 0, 127); // Completely fill the background of the new image with allocated color. imagefill($img, 0, 0, $color); // Restore transparency blending imagesavealpha($img, true); imagecopyresampled($img, $src, 0, 0, $this->x, $this->y, $this->maxwidth, $this->maxheight, $this->w, $this->h); @ImagePNG($img, null, 9); break; default: break; } imagedestroy($src); imagedestroy($img); if ($memoryLimitChanged == 1) { $memoryString = $memory . 'M'; ini_set('memory_limit', $memoryString); } }
/** * create image and draw string using GD * * @author Won-Kyu Park <*****@*****.**> * @since 2013/12/24 * @license GPLv2 */ function image_msg($font_size, $font_face, $text, $width = 40) { $wrap = wordwrap($text, $width, "\n", true); $wrap = rtrim($wrap); $strs = explode("\n", $wrap); if (empty($font_face)) { $w = imagefontwidth($font_size) * $width; $dy = imagefontheight($font_size); $h = $dy * count($strs); $im = ImageCreate($w, $h); $y = 0; } else { putenv('GDFONTPATH=' . getcwd() . '/data'); $w = 0; $h = 0; foreach ($strs as $str) { $bbox = imagettfbbox($font_size, 0, $font_face, $str); if ($bbox[2] > $w) { $w = $bbox[2]; } $h += $bbox[3] - $bbox[5]; } $dy = $bbox[3] - $bbox[5]; $h = $dy * count($strs); $im = ImageCreateTruecolor($w, $h); $y = $dy; } $bg = ImageColorAllocate($im, 255, 255, 255); // white background $pen = ImageColorAllocate($im, 0, 0, 0); // black imagefill($im, 0, 0, $bg); foreach ($strs as $str) { if (empty($font_face)) { ImageString($im, $font_size, 0, $y, $str, 1); } else { ImageTtfText($im, $font_size, 0, 0, $y, $pen, $font_face, $str); } $y += $dy; } return $im; }
<?php $im = ImageCreateTruecolor(300, 256); for ($r = 0; $r < 256; $r++) { $col = ImageColorAllocate($im, $r, 0, 0); ImageLine($im, 0, $r, 100, $r, $col); } for ($g = 0; $g < 256; $g++) { $col = ImageColorAllocate($im, 0, $g, 0); ImageLine($im, 100, 255 - $g, 200, 255 - $g, $col); } for ($b = 0; $b < 256; $b++) { $col = ImageColorAllocate($im, 0, 0, $b); ImageLine($im, 200, $b, 300, $b, $col); } Header('Content-Type: image/png'); ImagePNG($im);