/** * 生成された画像を保存する * * @return boolean * @access public * @static */ function save() { //保存先のディレクトリが存在しているかチェック $filePath = dirname($this->dstPath); if (!file_exists($filePath)) { mkdir($filePath); } if ($this->imageType == 'image/jpeg') { return imageJpeg($this->dstImage, $this->dstPath, $this->quality); } elseif ($this->imageType == 'image/gif') { return imageGif($this->dstImage, $this->dstPath); } elseif ($this->imageType == 'image/png') { return imagePng($this->dstImage, $this->dstPath); } }
public static function CreateGifThumbnail($imageDirectory, $imageName, $thumbDirectory, $thumbWidth) { $srcImg = ImageCreateFromGif($imageDirectory . $imageName); $origWidth = imagesx($srcImg); $origHeight = imagesy($srcImg); $ratio = $thumbWidth / $origWidth; $thumbHeight = $origHeight * $ratio; $thumbImg = imagecreatetruecolor($thumbWidth, $thumbHeight); imagecopyresampled($thumbImg, $srcImg, 0, 0, 0, 0, $thumbWidth, $thumbHeight, imagesx($srcImg), imagesy($srcImg)); imageGif($thumbImg, $thumbDirectory . "tn_" . $imageName); }
public static function saveFile($image = null, $destFile = null, $saveType = self::SAVE_JPG) { switch ($saveType) { case self::SAVE_GIF: return @imageGif($image, $destFile); case self::SAVE_JPG: return @imageJpeg($image, $destFile, self::SAVE_QUALITY); case self::SAVE_PNG: return @imagePng($image, $destFile); default: return false; } }
function AnimatedOut() { for ($i = 0; $i < ANIM_FRAMES; $i++) { $image = imageCreateTrueColor(imageSX($this->image), imageSY($this->image)); if (imageCopy($image, $this->image, 0, 0, 0, 0, imageSX($this->image), imageSY($this->image))) { Captcha::DoNoise($image, 200, 0); Ob_Start(); imageGif($image); imageDestroy($image); $f_arr[] = Ob_Get_Contents(); $d_arr[] = ANIM_DELAYS; Ob_End_Clean(); } } $GIF = new GIFEncoder($f_arr, $d_arr, 0, 2, -1, -1, -1, 'C_MEMORY'); return $GIF->GetAnimation(); }
private function createNewImage($newImg, $newName, $imgInfo) { switch ($imgInfo["type"]) { case 1: //gif $result = imageGif($newImg, $this->path . $newName); break; case 2: //jpg $result = imageJPEG($newImg, $this->path . $newName); break; case 3: //png $return = imagepng($newImg, $this->path . $newName); break; } imagedestroy($newImg); return $newName; }
function resizeImage($file, $max_x, $max_y, $forcePng = false) { if ($max_x <= 0 || $max_y <= 0) { $max_x = 5; $max_y = 5; } $src = BASEDIR . '/avatars/' . $file; list($width, $height, $type) = getImageSize($src); $scale = min($max_x / $width, $max_y / $height); $newWidth = $width * $scale; $newHeight = $height * $scale; $img = imagecreatefromstring(file_get_contents($src)); $black = imagecolorallocate($img, 0, 0, 0); $resizedImage = imageCreateTrueColor($newWidth, $newHeight); imagecolortransparent($resizedImage, $black); imageCopyResampled($resizedImage, $img, 0, 0, 0, 0, $newWidth, $newHeight, $width, $height); imageDestroy($img); unlink($src); if (!$forcePng) { switch ($type) { case IMAGETYPE_JPEG: imageJpeg($resizedImage, BASEDIR . '/avatars/' . $file); break; case IMAGETYPE_GIF: imageGif($resizedImage, BASEDIR . '/avatars/' . $file); break; case IMAGETYPE_PNG: imagePng($resizedImage, BASEDIR . '/avatars/' . $file); break; default: imagePng($resizedImage, BASEDIR . '/avatars/' . $file); break; } } else { imagePng($resizedImage, BASEDIR . '/avatars/' . $file . '.png'); } return; }
function save($filename, $type = 'png', $quality = 100) { $this->_build(); $this->_build_border(); switch ($type) { case 'gif': $ret = imageGif($this->_dest_image, $filename); break; case 'jpg': case 'jpeg': $ret = imageJpeg($this->_dest_image, $filename, $quality); break; case 'png': $ret = imagePng($this->_dest_image, $filename); break; default: $this->_error('Save: Invalid Format'); break; } if (!$ret) { $this->_error('Save: Unable to save'); } }
$alphabet = '1234567890abcdefghijkmnpqrstuvwxyz'; $string = substr(str_shuffle(str_repeat($alphabet, $length)), 0, $length); //���������� ����� $img = imagecreate(140, 50); //������� �������� if ($color == "red") { $color = imagecolorAllocate($img, 249, 183, 169); $border_color = imagecolorAllocate($img, 212, 12, 12); } else { $color = imagecolorAllocate($img, 217, 239, 185); $border_color = imagecolorAllocate($img, 160, 214, 81); } $text_color = imagecolorAllocate($img, 102, 102, 102); $�� = (imageSX($img) - 1 * strlen($string)) / 2; imagettftext($img, 23, -3, 10, 30, $text_color, 'font.TTF', $string); imageGif($img, "img/number.gif"); imageDestroy($img); $_SESSION["capcha"] = $string; ?> <div class="loginform" <?php if ($_SESSION['try'] > $num_for_lock) { echo 'style="background:url(../img/lock.png) no-repeat 60% 30%;"'; } ?> > <form method="post" action="index.php" > <fieldset > <p> <label for="login" class="top">����:</label><br /> <input type="text" name="login" id="login" tabindex="1" class="field" value="<?php echo $_POST['login'];
<?php session_start(); $alpha = "0123456789"; $secret = ""; $old_user_id; for ($i = 0; $i < 5; $i++) { $secret .= $alpha[rand(0, strlen($alpha) - 1)]; } $_SESSION['secret'] = $secret; $im = imagecreate(80, 31); imageColorAllocate($im, 255, 255, 255); $textcolor = imagecolorallocate($im, 0, 0, 0); imagestring($im, 5, 10, 10, $secret, $textcolor); imageGif($im); header("Content-Type: image/gif");
function imageResize($params, $filePath) { $params["WIDTH"] = !isset($params["WIDTH"]) ? 100 : intval($params["WIDTH"]); $params["HEIGHT"] = !isset($params["HEIGHT"]) ? 100 : intval($params["HEIGHT"]); $params["MODE"] = !isset($params["MODE"]) ? 'inv' : strtolower($params["MODE"]); $params["QUALITY"] = (!isset($params["QUALITY"]) || intval($params["QUALITY"]) <= 0 ) ? 100 : $params["QUALITY"]; $params["HIQUALITY"] = !isset($params["HIQUALITY"]) ? (($params["WIDTH"] <= 200 || $params["HEIGHT"] <= 200) ? 1 : 0 ) : 0; $imageType = getFileExtension($filePath); $pathToOriginalFile = "{$_SERVER["DOCUMENT_ROOT"]}/{$filePath}"; if (!file_exists($pathToOriginalFile)) return; $salt = md5(strtolower($filePath) . implode('_', $params) . $SETSTYLE); $salt = substr($salt, 0, 3) . '/'; $filename = basename($filePath); $pathToFile = $salt . $filename; // если изображение существует if (is_file(CHACHE_IMG_PATH . $pathToFile) == true) { if ($_REQUEST["clear_cache"] == 'IMAGE') { //при очистке кэша unlink(RETURN_IMG_PATH . $pathToFile); } else { return RETURN_IMG_PATH . $pathToFile; } } CheckDirPath(CHACHE_IMG_PATH . $salt); $i = getImageSize($pathToOriginalFile); if (intval($params["WIDTH"]) == 0) $params["WIDTH"] = intval($params["HEIGHT"] / $i[1] * $i[0]); if (intval($params["HEIGHT"]) == 0) $params["HEIGHT"] = intval($params["WIDTH"] / $i[0] * $i[1]); //если вырезаться будет cut проверка размеров if (($params["WIDTH"] > $i[0] || $params["HEIGHT"] > $i[1]) && ($params["MODE"] != "in" && $params["MODE"] != "inv")) { $params["WIDTH"] = $i[0]; $params["HEIGHT"] = $i[1]; } $im = ImageCreateTrueColor($params["WIDTH"], $params["HEIGHT"]); imageAlphaBlending($im, false); switch (strtolower($imageType)) { case 'gif' : $i0 = ImageCreateFromGif($pathToOriginalFile); $icolor = imagecolorallocate($im, 255, 255, 255); imagefill($im, 0, 0, $icolor); break; case 'jpg' : case 'jpeg' : $i0 = ImageCreateFromJpeg($pathToOriginalFile); $icolor = imagecolorallocate($im, 255, 255, 255); imagefill($im, 0, 0, $icolor); break; case 'png' : $i0 = ImageCreateFromPng($pathToOriginalFile); $icolor = imagecolorallocate($im, 255, 255, 255); imagefill($im, 0, 0, $icolor); break; } if (!($i[0] == $params["WIDTH"] && $i[1] == $params["HEIGHT"] && !$SETSTYLE)) { switch (strtolower($params["MODE"])) { case 'cut' : $k_x = $i [0] / $params["WIDTH"]; $k_y = $i [1] / $params["HEIGHT"]; if ($k_x > $k_y) $k = $k_y; else $k = $k_x; $pn["WIDTH"] = $i [0] / $k; $pn["HEIGHT"] = $i [1] / $k; $x = ($params["WIDTH"] - $pn["WIDTH"]) / 2; $y = ($params["HEIGHT"] - $pn["HEIGHT"]) / 2; imageCopyResampled($im, $i0, $x, $y, 0, 0, $pn["WIDTH"] + 2, $pn["HEIGHT"] + 2, $i[0], $i[1]); // ( dst_im, src_im, dstX, dstY, srcX, srcY, int dstW, int dstH, int srcW, int srcH) break; //вписана в квадрат без маштабирования (картинка может быть увеличена больше своего размера) case 'in' : if (($i [0] < $params["WIDTH"]) && ($i [1] < $params["HEIGHT"])) { $k_x = 1; $k_y = 1; } else { $k_x = $i[0] / $params["WIDTH"]; $k_y = $i[1] / $params["HEIGHT"]; } if ($k_x < $k_y) $k = $k_y; else $k = $k_x; $pn["WIDTH"] = intval($i[0] / $k); $pn["HEIGHT"] = intval($i[1] / $k); $x = intval(($params["WIDTH"] - $pn["WIDTH"]) / 2); $y = intval(($params["HEIGHT"] - $pn["HEIGHT"]) / 2); imageCopyResampled($im, $i0, $x, $y, 0, 0, $pn["WIDTH"], $pn["HEIGHT"], $i[0], $i[1]); // 1 первый параметр изборажение источник // 2 изображение которое вставляется // 3 4 -х и у с какой точки будет вставятся в изображении источник // 5 6 - ширина и высота куда будет вписано изображение break; //вписана в квадрат с маштабированием (картинка может быть увеличена) case 'inv' : $k_x = $i [0] / $params["WIDTH"]; $k_y = $i [1] / $params["HEIGHT"]; if ($k_x < $k_y) $k = $k_y; else $k = $k_x; $pn["WIDTH"] = $i [0] / $k; $pn["HEIGHT"] = $i [1] / $k; $x = ($params["WIDTH"] - $pn["WIDTH"]) / 2; $y = ($params["HEIGHT"] - $pn["HEIGHT"]) / 2; imageCopyResampled($im, $i0, $x, $y, 0, 0, $pn["WIDTH"], $pn["HEIGHT"], $i[0], $i[1]); if ($params["WIDTH"] == 55 && $params["HEIGHT"] == 45) { imageAlphaBlending($im, true); $waterMark = ImageCreateFromPng($_SERVER["DOCUMENT_ROOT"] . "/img/video.png"); imageCopyResampled($im, $waterMark, 0, 0, 0, 0, $params["WIDTH"], $params["HEIGHT"], $params["WIDTH"], $params["HEIGHT"]); } break; case 'width' : $factor = $i[1] / $i[0]; // определяем пропорцию height / width if ($factor > 1.35) { $pn["WIDTH"] = $params["WIDTH"]; $scale_factor = $i[0] / $pn["WIDTH"]; // коэфффициент масштабирования $pn["HEIGHT"] = ceil($i[1] / $scale_factor); $x = 0; $y = 0; if (($params["HEIGHT"] / $pn["HEIGHT"]) < 0.6) { //echo 100 / ($pn["HEIGHT"] * 100) / ($params["HEIGHT"] *1.5); $pn["HEIGHT"] = (100 / (($pn["HEIGHT"] * 100) / ($params["HEIGHT"] * 1.3))) * $pn["HEIGHT"]; $newKoef = $i[1] / $pn["HEIGHT"]; $pn["WIDTH"] = $i[0] / $newKoef; $x = ($params["WIDTH"] - $pn["WIDTH"]) / 2; //$y = ($params["HEIGHT"] - $pn["HEIGHT"]) / 2; } imageCopyResampled($im, $i0, $x, $y, 0, 0, $pn["WIDTH"], $pn["HEIGHT"], $i[0], $i[1]); } else { if (($i [0] < $params["WIDTH"]) && ($i [1] < $params["HEIGHT"])) { $k_x = 1; $k_y = 1; } else { $k_x = $i [0] / $params["WIDTH"]; $k_y = $i [1] / $params["HEIGHT"]; } if ($k_x < $k_y) $k = $k_y; else $k = $k_x; $pn["WIDTH"] = $i [0] / $k; $pn["HEIGHT"] = $i [1] / $k; $x = ($params["WIDTH"] - $pn["WIDTH"]) / 2; $y = ($params["HEIGHT"] - $pn["HEIGHT"]) / 2; imageCopyResampled($im, $i0, $x, $y, 0, 0, $params["MODE"], $pn["HEIGHT"], $i[0], $i[1]); } break; default : imageCopyResampled($im, $i0, 0, 0, 0, 0, $params["WIDTH"], $params["HEIGHT"], $i[0], $i[1]); break; } if ($params["HIQUALITY"]) { $sharpenMatrix = array ( array(-1.2, -1, -1.2), array(-1, 20, -1), array(-1.2, -1, -1.2) ); // calculate the sharpen divisor $divisor = array_sum(array_map('array_sum', $sharpenMatrix)); $offset = 0; // apply the matrix imageconvolution($im, $sharpenMatrix, $divisor, $offset); } switch (strtolower($imageType)) { case 'gif' :imageSaveAlpha($im, true); @imageGif($im, CHACHE_IMG_PATH . $pathToFile); break; case 'jpg' : case 'jpeg' : @imageJpeg($im, CHACHE_IMG_PATH . $pathToFile, $params["QUALITY"]); break; case 'png' : imageSaveAlpha($im, true); @imagePng($im, CHACHE_IMG_PATH . $pathToFile); break; } } else { copy($pathToOriginalFile, CHACHE_IMG_PATH . $pathToFile); } return RETURN_IMG_PATH . $pathToFile; }
/** * Write a gif image to file * @param File file of the image * @param resource internal PHP image resource */ public function write(File $file, $resource) { $this->checkIfWriteIsPossible($file, $resource, 'gif'); imageGif($resource, $file->getAbsolutePath()); }
/** * 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; } }
/** * Creates error image based on gfx/notfound_thumb.png * Requires GD lib enabled, otherwise it will exit with the three * textstrings outputted as text. Outputs the image stream to browser and exits! * * @param string $filename Name of the file * @param string $textline1 Text line 1 * @param string $textline2 Text line 2 * @param string $textline3 Text line 3 * @return void * @throws \RuntimeException * * @internal Don't use this method from outside the LocalImageProcessor! */ public function getTemporaryImageWithText($filename, $textline1, $textline2, $textline3) { if (!$GLOBALS['TYPO3_CONF_VARS']['GFX']['gdlib']) { throw new \RuntimeException('TYPO3 Fatal Error: No gdlib. ' . $textline1 . ' ' . $textline2 . ' ' . $textline3, 1270853952); } // Creates the basis for the error image if ($GLOBALS['TYPO3_CONF_VARS']['GFX']['gdlib_png']) { $im = imagecreatefrompng(PATH_typo3 . 'gfx/notfound_thumb.png'); } else { $im = imagecreatefromgif(PATH_typo3 . 'gfx/notfound_thumb.gif'); } // Sets background color and print color. $white = imageColorAllocate($im, 255, 255, 255); $black = imageColorAllocate($im, 0, 0, 0); // Prints the text strings with the build-in font functions of GD $x = 0; $font = 0; if ($textline1) { imagefilledrectangle($im, $x, 9, 56, 16, $white); imageString($im, $font, $x, 9, $textline1, $black); } if ($textline2) { imagefilledrectangle($im, $x, 19, 56, 26, $white); imageString($im, $font, $x, 19, $textline2, $black); } if ($textline3) { imagefilledrectangle($im, $x, 29, 56, 36, $white); imageString($im, $font, $x, 29, substr($textline3, -14), $black); } // Outputting the image stream and exit if ($GLOBALS['TYPO3_CONF_VARS']['GFX']['gdlib_png']) { imagePng($im, $filename); } else { imageGif($im, $filename); } }
/** * Создание превью средствами gd * @param $src_file исходный файл * @param $dst_file файл с результатом * @param max_w максимальная ширина * @param max_h максимальная высота * @return bool */ private function image_constrain_gd($src_file, $dst_file, $max_w, $max_h, $watermark = null, $watermark_offet_x = 0, $watermark_offet_y = 0, $watermark_opacity = 1) { $quality = 100; // Параметры исходного изображения @(list($src_w, $src_h, $src_type) = array_values(getimagesize($src_file))); $src_type = image_type_to_mime_type($src_type); if (empty($src_w) || empty($src_h) || empty($src_type)) { return false; } // Нужно ли обрезать? if (!$watermark && $src_w <= $max_w && $src_h <= $max_h) { // Нет - просто скопируем файл if (!copy($src_file, $dst_file)) { return false; } return true; } // Размеры превью при пропорциональном уменьшении @(list($dst_w, $dst_h) = $this->calc_contrain_size($src_w, $src_h, $max_w, $max_h)); // Читаем изображение switch ($src_type) { case 'image/jpeg': $src_img = imageCreateFromJpeg($src_file); break; case 'image/gif': $src_img = imageCreateFromGif($src_file); break; case 'image/png': $src_img = imageCreateFromPng($src_file); imagealphablending($src_img, true); break; default: return false; } if (empty($src_img)) { return false; } $src_colors = imagecolorstotal($src_img); // create destination image (indexed, if possible) if ($src_colors > 0 && $src_colors <= 256) { $dst_img = imagecreate($dst_w, $dst_h); } else { $dst_img = imagecreatetruecolor($dst_w, $dst_h); } if (empty($dst_img)) { return false; } $transparent_index = imagecolortransparent($src_img); if ($transparent_index >= 0 && $transparent_index <= 128) { $t_c = imagecolorsforindex($src_img, $transparent_index); $transparent_index = imagecolorallocate($dst_img, $t_c['red'], $t_c['green'], $t_c['blue']); if ($transparent_index === false) { return false; } if (!imagefill($dst_img, 0, 0, $transparent_index)) { return false; } imagecolortransparent($dst_img, $transparent_index); } elseif ($src_type === 'image/png') { if (!imagealphablending($dst_img, false)) { return false; } $transparency = imagecolorallocatealpha($dst_img, 0, 0, 0, 127); if (false === $transparency) { return false; } if (!imagefill($dst_img, 0, 0, $transparency)) { return false; } if (!imagesavealpha($dst_img, true)) { return false; } } // resample the image with new sizes if (!imagecopyresampled($dst_img, $src_img, 0, 0, 0, 0, $dst_w, $dst_h, $src_w, $src_h)) { return false; } // Watermark if (!empty($watermark) && is_readable($watermark)) { $overlay = imagecreatefrompng($watermark); // Get the size of overlay $owidth = imagesx($overlay); $oheight = imagesy($overlay); $watermark_x = min(($dst_w - $owidth) * $watermark_offet_x / 100, $dst_w); $watermark_y = min(($dst_h - $oheight) * $watermark_offet_y / 100, $dst_h); imagecopy($dst_img, $overlay, $watermark_x, $watermark_y, 0, 0, $owidth, $oheight); //imagecopymerge($dst_img, $overlay, $watermark_x, $watermark_y, 0, 0, $owidth, $oheight, $watermark_opacity*100); } // recalculate quality value for png image if ('image/png' === $src_type) { $quality = round($quality / 100 * 10); if ($quality < 1) { $quality = 1; } elseif ($quality > 10) { $quality = 10; } $quality = 10 - $quality; } // Сохраняем изображение switch ($src_type) { case 'image/jpeg': return imageJpeg($dst_img, $dst_file, $quality); case 'image/gif': return imageGif($dst_img, $dst_file, $quality); case 'image/png': imagesavealpha($dst_img, true); return imagePng($dst_img, $dst_file, $quality); default: return false; } }
/** * Creates error image based on gfx/notfound_thumb.png * Requires GD lib enabled, otherwise it will exit with the three * textstrings outputted as text. Outputs the image stream to browser and exits! * * @param string $filename Name of the file * @param string $textline1 Text line 1 * @param string $textline2 Text line 2 * @param string $textline3 Text line 3 * @return void * @throws \RuntimeException */ public function getTemporaryImageWithText($filename, $textline1, $textline2, $textline3) { if (empty($GLOBALS['TYPO3_CONF_VARS']['GFX']['gdlib'])) { throw new \RuntimeException('TYPO3 Fatal Error: No gdlib. ' . $textline1 . ' ' . $textline2 . ' ' . $textline3, 1270853952); } // Creates the basis for the error image $basePath = ExtensionManagementUtility::extPath('core') . 'Resources/Public/Images/'; if (!empty($GLOBALS['TYPO3_CONF_VARS']['GFX']['gdlib_png'])) { $im = imagecreatefrompng($basePath . 'NotFound.png'); } else { $im = imagecreatefromgif($basePath . 'NotFound.gif'); } // Sets background color and print color. $white = imageColorAllocate($im, 255, 255, 255); $black = imageColorAllocate($im, 0, 0, 0); // Prints the text strings with the build-in font functions of GD $x = 0; $font = 0; if ($textline1) { imagefilledrectangle($im, $x, 9, 56, 16, $white); imageString($im, $font, $x, 9, $textline1, $black); } if ($textline2) { imagefilledrectangle($im, $x, 19, 56, 26, $white); imageString($im, $font, $x, 19, $textline2, $black); } if ($textline3) { imagefilledrectangle($im, $x, 29, 56, 36, $white); imageString($im, $font, $x, 29, substr($textline3, -14), $black); } // Outputting the image stream and exit if (!empty($GLOBALS['TYPO3_CONF_VARS']['GFX']['gdlib_png'])) { imagePng($im, $filename); } else { imageGif($im, $filename); } }
<?php require_once dirname(__FILE__) . '/../../config/config.inc.php'; require_once dirname(__FILE__) . '/../../init.php'; require_once dirname(__FILE__) . '/menu.class.php'; $img = imageCreateFromGif(dirname(__FILE__) . '/gfx/menu/menu_orig.gif'); if (isset($_GET['color']) && strlen($_GET['color']) === 6) { $color = Tools::getValue('color'); $light = -150; if (isset($_GET['light']) && ((int) $_GET['light'] >= 0 && (int) $_GET['light'] <= 200)) { $light = (int) $_GET['light'] + $light; } else { $light += 100; } Menu::colorize($img, $color, $light); } if (isset($_GET['preview'])) { header('Content-type: image/gif'); imageGif($img); } imagedestroy($img);
function showCartoonfy($p_ext, $p_dis) { switch (strtolower($p_ext)) { case "jpg": header('Content-type:image/' . strtolower($p_ext)); if ($p_dis) { header('Content-disposition:attachment;filename=' . time() . '.' . strtolower($p_ext)); } imageJpeg($this->i1); break; case "jpeg": header('Content-type:image/' . strtolower($p_ext)); if ($p_dis) { header('Content-disposition:attachment;filename=' . time() . '.' . strtolower($p_ext)); } imageJpeg($this->i1); break; case "gif": header('Content-type:image/' . strtolower($p_ext)); if ($p_dis) { header('Content-disposition:attachment;filename=' . time() . '.' . strtolower($p_ext)); } imageGif($this->i1); break; case "png": header('Content-type:image/' . strtolower($p_ext)); if ($p_dis) { header('Content-disposition:attachment;filename=' . time() . '.' . strtolower($p_ext)); } imagePng($this->i1); break; default: print "<b>" . $p_ext . "</b> is not supported image format!"; exit; } imageDestroy($this->i1); }
/* Подготовка к работе */ //imageAntiAlias($i, true); /* $red = imageColorAllocate($i, 255, 0, 0); $white = imageColorAllocate($i, 0xFF, 0xFF, 0xFF); $black = imageColorAllocate($i, 0, 0, 0); $green = imageColorAllocate($i, 0, 255, 0); $blue = imageColorAllocate($i, 0, 0, 255); $grey = imageColorAllocate($i, 192, 192, 192); */ //imageFill($i, 0, 0, $grey); /* Рисуем примитивы */ //imageSetPixel($i, 10, 10, $black); //imageLine($i, 20, 20, 80, 280, $red); //imageRectangle($i, 20, 20, 80, 280, $blue); //$points = array(0, 0, 100, 200, 300, 200); //imagePolygon($i, $points, 3, $green); //imageEllipse($i, 200, 150, 300, 200, $red); //imageArc($i, 200, 150, 300, 200, 0, 40, $black); //imageFilledArc($i, 200, 150, 300, 200, 0, 40, $red, IMG_ARC_PIE); /* Рисуем текст */ //imageString($i, 5, 150, 200, 'PHP5', $black); //imageChar($i, 3, 20, 20, 'PHP5', $blue); //imageTtfText($i, 30, 10, 300, 150, $green,'arial.ttf', 'PHP5'); /* Отдаем изображение */ header("Content-type: image/gif"); imageGif($i); //header("Content-type: image/png"); //imagePng($i); //header("Content-type: image/jpg"); //imageJpeg($i);
/** * This function resizes given image, if it has bigger dimensions, than we need and saves it (also makes chmod 0777 on the file) ... * It uses GD or ImageMagick, setting is available to change in CL's config file. * * @param string $inputFileName the input file to work with * @param string $outputFileName the file to write into the final (resized) image * @param integer $maxNewWidth maximal width of image * @param integer $maxNewHeight maximal height of image * @return bool TRUE, if everything was successful (resize and chmod), else FALSE */ function resize($inputFileName, $outputFileName, $maxNewWidth, $maxNewHeight) { $imageInfo = getimagesize($inputFileName); $fileType = $imageInfo['mime']; $extension = strtolower(str_replace('.', '', substr($outputFileName, -4))); $originalWidth = $imageInfo[0]; $originalHeight = $imageInfo[1]; if ($originalWidth > $maxNewWidth or $originalHeight > $maxNewHeight) { $newWidth = $maxNewWidth; $newHeight = $originalHeight / ($originalWidth / $maxNewWidth); if ($newHeight > $maxNewHeight) { $newHeight = $maxNewHeight; $newWidth = $originalWidth / ($originalHeight / $maxNewHeight); } $newWidth = ceil($newWidth); $newHeight = ceil($newHeight); } else { $newWidth = $originalWidth; $newHeight = $originalHeight; } $ok = FALSE; if (CL::getConf('CL_Images/engine') == 'imagick-cli') { exec("convert -thumbnail " . $newWidth . "x" . $newHeight . " " . $inputFileName . " " . $outputFileName); $ok = TRUE; } elseif (CL::getConf('CL_Images/engine') == 'imagick-php') { $image = new Imagick($inputFileName); $image->thumbnailImage($newWidth, $newHeight); $ok = (bool) $image->writeImage($outputFileName); $image->clear(); $image->destroy(); } else { $out = imageCreateTrueColor($newWidth, $newHeight); switch (strtolower($fileType)) { case 'image/jpeg': $source = imageCreateFromJpeg($inputFileName); break; case 'image/png': $source = imageCreateFromPng($inputFileName); break; case 'image/gif': $source = imageCreateFromGif($inputFileName); break; default: break; } imageCopyResized($out, $source, 0, 0, 0, 0, $newWidth, $newHeight, $originalWidth, $originalHeight); switch (strtolower($extension)) { case 'jpg': case 'jpeg': if (imageJpeg($out, $outputFileName)) { $ok = TRUE; } break; case 'png': if (imagePng($out, $outputFileName)) { $ok = TRUE; } break; case 'gif': if (imageGif($out, $outputFileName)) { $ok = TRUE; } break; default: break; } imageDestroy($out); imageDestroy($source); } if ($ok and chmod($outputFileName, 0777)) { return TRUE; } else { return FALSE; } }
/** * Writes the input GDlib image pointer to file * * @param pointer The GDlib image resource pointer * @param string The filename to write to * @param integer The image quality (for JPEGs) * @return boolean The output of either imageGif, imagePng or imageJpeg based on the filename to write * @see maskImageOntoImage(), scale(), output() */ function ImageWrite($destImg, $theImage, $quality = 0) { imageinterlace($destImg, 0); $ext = strtolower(substr($theImage, strrpos($theImage, '.') + 1)); $result = FALSE; switch ($ext) { case 'jpg': case 'jpeg': if (function_exists('imageJpeg')) { if ($quality == 0) { $quality = $this->jpegQuality; } $result = imageJpeg($destImg, $theImage, $quality); } break; case 'gif': if (function_exists('imageGif')) { imagetruecolortopalette($destImg, TRUE, 256); $result = imageGif($destImg, $theImage); } break; case 'png': if (function_exists('imagePng')) { $result = ImagePng($destImg, $theImage); } break; } if ($result) { t3lib_div::fixPermissions($theImage); } return $result; }
/** * Creates a font-preview thumbnail. * This means a PNG/GIF file with the text "AaBbCc...." set with the font-file given as input and in various sizes to show how the font looks * Requires GD lib enabled. * Outputs the image stream to browser and exits! * * @param string $font The filepath to the font file (absolute, probably) * @return void * @todo Define visibility */ public function fontGif($font) { if (!$GLOBALS['TYPO3_CONF_VARS']['GFX']['gdlib']) { throw new \RuntimeException('TYPO3 Fatal Error: No gdlib.', 1270853953); } // Create image and set background color to white. $im = imageCreate(250, 76); $white = imageColorAllocate($im, 255, 255, 255); $col = imageColorAllocate($im, 0, 0, 0); // The test string and offset in x-axis. $string = 'AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZzÆæØøÅåÄäÖöÜüß'; $x = 13; // Print (with non-ttf font) the size displayed imagestring($im, 1, 0, 2, '10', $col); imagestring($im, 1, 0, 15, '12', $col); imagestring($im, 1, 0, 30, '14', $col); imagestring($im, 1, 0, 47, '18', $col); imagestring($im, 1, 0, 68, '24', $col); // Print with ttf-font the test string imagettftext($im, GeneralUtility::freetypeDpiComp(10), 0, $x, 8, $col, $font, $string); imagettftext($im, GeneralUtility::freetypeDpiComp(12), 0, $x, 21, $col, $font, $string); imagettftext($im, GeneralUtility::freetypeDpiComp(14), 0, $x, 36, $col, $font, $string); imagettftext($im, GeneralUtility::freetypeDpiComp(18), 0, $x, 53, $col, $font, $string); imagettftext($im, GeneralUtility::freetypeDpiComp(24), 0, $x, 74, $col, $font, $string); // Output PNG or GIF based on $GLOBALS['TYPO3_CONF_VARS']['GFX']['gdlib_png'] if ($GLOBALS['TYPO3_CONF_VARS']['GFX']['gdlib_png']) { header('Content-type: image/png'); imagePng($im); } else { header('Content-type: image/gif'); imageGif($im); } imagedestroy($im); die; }
/** * Function: encodeImage * * Encodes the given image using the GD image encoding routines. * Supported formats are gif, jpg and png (default). * * Parameters: * * image - GD image to be encoded. * format - String that defines the encoding format. Default is png. */ static function encodeImage($image, $format = null) { if ($format == "gif") { return imageGif($image); } else { if ($format == "jpg") { return imageJpeg($image); } else { return imagePng($image); } } }
/** * Compressing a GIF file if not already LZW compressed. * This function is a workaround for the fact that ImageMagick and/or GD does not compress GIF-files to their minimun size (that is RLE or no compression used) * * The function takes a file-reference, $theFile, and saves it again through GD or ImageMagick in order to compress the file * GIF: * If $type is not set, the compression is done with ImageMagick (provided that $GLOBALS['TYPO3_CONF_VARS']['GFX']['im_path_lzw'] is pointing to the path of a lzw-enabled version of 'convert') else with GD (should be RLE-enabled!) * If $type is set to either 'IM' or 'GD' the compression is done with ImageMagick and GD respectively * PNG: * No changes. * * $theFile is expected to be a valid GIF-file! * The function returns a code for the operation. * Usage: 9 * * @param string Filepath * @param string See description of function * @return string Returns "GD" if GD was used, otherwise "IM" if ImageMagick was used. If nothing done at all, it returns empty string. */ public static function gif_compress($theFile, $type) { $gfxConf = $GLOBALS['TYPO3_CONF_VARS']['GFX']; $returnCode = ''; if ($gfxConf['gif_compress'] && strtolower(substr($theFile, -4, 4)) == '.gif') { // GIF... if (($type == 'IM' || !$type) && $gfxConf['im'] && $gfxConf['im_path_lzw']) { // IM $cmd = self::imageMagickCommand('convert', '"' . $theFile . '" "' . $theFile . '"', $gfxConf['im_path_lzw']); exec($cmd); $returnCode = 'IM'; } elseif (($type == 'GD' || !$type) && $gfxConf['gdlib'] && !$gfxConf['gdlib_png']) { // GD $tempImage = imageCreateFromGif($theFile); imageGif($tempImage, $theFile); imageDestroy($tempImage); $returnCode = 'GD'; } } return $returnCode; }
function redimage($src, $dest, $dw = false, $dh = false, $stamp = false) { // detect file type (could be a lot better) if (is_array($src)) { $type_src = strtoupper(substr($src['name'], -3)); $src = $src['tmp_name']; } else { $type_src = strtoupper(substr($src, -3)); } $type_dest = strtoupper(substr($dest, -3)); // read source image switch ($type_src) { case 'JPG': $src_img = ImageCreateFromJpeg($src); break; case 'PEG': $src_img = ImageCreateFromJpeg($src); break; case 'GIF': $src_img = ImageCreateFromGif($src); break; case 'PNG': $src_img = imageCreateFromPng($src); break; case 'BMP': $src_img = imageCreatefromWBmp($src); break; } // get it's info $size = GetImageSize($src); $sw = $size[0]; $sh = $size[1]; /* // get it's info $size = GetImageSize ($src); $fw = $size[0]; $fh = $size[1]; // ROGNE the picture from the top left pixel's color $rogne_color = imagecolorat ($src_img, 0, 0); $rogne_point = array ($fh, 0, 0, $fw); for ($x = 0; $x < $fw; $x ++){ for ($y = 0; $y < $fh; $y ++){ if (imagecolorat ($src_img, $x, $y) != $rogne_color){ $rogne_point[0] = ($rogne_point[0] > $y)?$y:$rogne_point[0]; $rogne_point[1] = ($rogne_point[1] < $x)?$x:$rogne_point[1]; $rogne_point[2] = ($rogne_point[2] < $y)?$y:$rogne_point[2]; $rogne_point[3] = ($rogne_point[3] > $x)?$x:$rogne_point[3]; } } } $sw = $rogne_point[1] - $rogne_point[3]; $sh = $rogne_point[2] - $rogne_point[0]; $rogne_img = ImageCreateTrueColor ($sw, $sh); ImageCopyResampled ($rogne_img, $src_img, 0, 0, $rogne_point[3], $rogne_point[0], $fw, $fh, $fw, $fh); $src_img = $rogne_img; */ // do not redim the pic if ($dw == false && $dh == false) { $dest_img = ImageCreateTrueColor($sw, $sh); ImageCopyResampled($dest_img, $src_img, 0, 0, 0, 0, $sw, $sh, $sw, $sh); } else { // redim the pix with dest W as max Side if ($dw != 0 && $dh == false) { if ($sw == $sh) { $dh = $dw; } else { if ($sw > $sh) { $dh = round($dw / $sw * $sh); } else { $dh = $dw; $dw = round($dh / $sh * $sw); } } $dest_img = ImageCreateTrueColor($dw, $dh); ImageCopyResampled($dest_img, $src_img, 0, 0, 0, 0, $dw, $dh, $sw, $sh); } else { // redim the pic according to dest W or dest H if ($dw == 0 || $dh == 0) { if ($dw == 0) { $dw = round($dh / $sh * $sw); } else { if ($dh == 0) { $dh = round($dw / $sw * $sh); } } $dest_img = ImageCreateTrueColor($dw, $dh); ImageCopyResampled($dest_img, $src_img, 0, 0, 0, 0, $dw, $dh, $sw, $sh); } else { if ($sw / $sh < $dw / $dh) { $tw = $sw; $th = round($sw / $dw * $dh); $x = 0; $y = round(($sh - $th) / 2); $temp_img = ImageCreateTrueColor($tw, $th); $dest_img = ImageCreateTrueColor($dw, $dh); ImageCopyResampled($temp_img, $src_img, 0, 0, $x, $y, $sw, $sh, $sw, $sh); ImageCopyResampled($dest_img, $temp_img, 0, 0, 0, 0, $dw, $dh, $tw, $th); ImageDestroy($temp_img); } else { $tw = $sw; $th = round($sw * ($dh / $dw)); $x = 0; $y = round(($th - $sh) / 2); $temp_img = ImageCreateTrueColor($tw, $th); $dest_img = ImageCreateTrueColor($dw, $dh); imagefill($temp_img, 0, 0, imagecolorallocate($dest_img, 0, 0, 0)); ImageCopyResampled($temp_img, $src_img, $x, $y, 0, 0, $sw, $sh, $sw, $sh); ImageCopyResampled($dest_img, $temp_img, 0, 0, 0, 0, $dw, $dh, $tw, $th); ImageDestroy($temp_img); } } } } if ($stamp != false) { // detect file type (could be a lot better) $type_stamp = strtoupper(substr($stamp, -3)); // read stamp switch ($type_stamp) { case 'JPG': $stamp_img = ImageCreateFromJpeg($stamp); break; case 'PEG': $stamp_img = ImageCreateFromJpeg($stamp); break; case 'GIF': $stamp_img = ImageCreateFromGif($stamp); break; case 'PNG': $stamp_img = imageCreateFromPng($stamp); break; case 'BMP': $stamp_img = imageCreatefromWBmp($stamp); break; } // get it's info $size = GetImageSize($stamp); $stw = $size[0]; $sth = $size[1]; $sx = $dw - $stw; $sy = $dh - $sth; imagecolortransparent($stamp_img, imageColorAllocate($stamp_img, 0, 0, 0)); imagecopy($dest_img, $stamp_img, $sx, $sy, 0, 0, $stw, $sth); } // free destination if (file_exists($dest_img)) { unlink($dest_img); } // save dest image switch ($type_dest) { case 'JPG': imageJpeg($dest_img, $dest, 90); break; case 'PEG': imageJpeg($dest_img, $dest, 90); break; case 'GIF': imageGif($dest_img, $dest, 90); break; case 'PNG': imagePng($dest_img, $dest, 90); break; case 'BMP': imageWBmp($dest_img, $dest, 90); break; } // free memory imageDestroy($src_img); ImageDestroy($dest_img); }
static function redimage($src, $dest, $dw = false, $dh = false, $loose = false, $stamp = false) { // detect file type (could be a lot better) if (is_array($src)) { $type_src = strtoupper(substr($src['name'], -3)); $src = $src['tmp_name']; } else { $type_src = strtoupper(substr($src, -3)); } $type_dest = strtoupper(substr($dest, -3)); // read source image switch ($type_src) { case 'JPG': case 'PEG': $src_img = ImageCreateFromJpeg($src); break; case 'GIF': $src_img = ImageCreateFromGif($src); break; case 'PNG': $src_img = imageCreateFromPng($src); break; case 'BMP': $src_img = imageCreatefromWBmp($src); break; } // get it's info $size = GetImageSize($src); $sw = $size[0]; $sh = $size[1]; // do not redim the pic if ($dw == false && $dh == false) { $dest_img = ImageCreateTrueColor($sw, $sh); ImageCopyResampled($dest_img, $src_img, 0, 0, 0, 0, $sw, $sh, $sw, $sh); } elseif ($dw != 0 && $dh == false) { if ($sw == $sh) { $dh = $dw; } elseif ($sw > $sh) { $dh = round($dw / $sw * $sh); } else { $dh = $dw; $dw = round($dh / $sh * $sw); } $dest_img = ImageCreateTrueColor($dw, $dh); ImageCopyResampled($dest_img, $src_img, 0, 0, 0, 0, $dw, $dh, $sw, $sh); } elseif ($dw == 0 || $dh == 0) { if ($dw == 0) { $dw = round($dh / $sh * $sw); } elseif ($dh == 0) { $dh = round($dw / $sw * $sh); } $dest_img = ImageCreateTrueColor($dw, $dh); ImageCopyResampled($dest_img, $src_img, 0, 0, 0, 0, $dw, $dh, $sw, $sh); } else { $test = $loose ? $sw / $sh > $dw / $dh : $sw / $sh < $dw / $dh; if ($test) { $th = $sh; $tw = round($sh * ($dw / $dh)); $x = round(($tw - $sw) / 2); $y = 0; } else { $tw = $sw; $th = round($sw * ($dh / $dw)); $x = 0; $y = round(($th - $sh) / 2); } $temp_img = ImageCreateTrueColor($tw, $th); $dest_img = ImageCreateTrueColor($dw, $dh); imagefill($temp_img, 0, 0, imagecolorallocate($dest_img, 255, 255, 255)); ImageCopyResampled($temp_img, $src_img, $x, $y, 0, 0, $sw, $sh, $sw, $sh); ImageCopyResampled($dest_img, $temp_img, 0, 0, 0, 0, $dw, $dh, $tw, $th); ImageDestroy($temp_img); } if ($stamp != false) { // detect file type (could be a lot better) $type_stamp = strtoupper(substr($stamp, -3)); // read stamp switch ($type_stamp) { case 'JPG': case 'PEG': $stamp_img = ImageCreateFromJpeg($stamp); break; case 'GIF': $stamp_img = ImageCreateFromGif($stamp); break; case 'PNG': $stamp_img = imageCreateFromPng($stamp); break; case 'BMP': $stamp_img = imageCreatefromWBmp($stamp); break; } // get it's info $size = GetImageSize($stamp); $stw = $size[0]; $sth = $size[1]; $sx = $dw - $stw; $sy = $dh - $sth; imagecolortransparent($stamp_img, imageColorAllocate($stamp_img, 0, 0, 0)); imagecopy($dest_img, $stamp_img, $sx, $sy, 0, 0, $stw, $sth); } // free destination if (file_exists($dest)) { unlink($dest); } // save dest image switch ($type_dest) { case 'JPG': case 'PEG': imageJpeg($dest_img, $dest, 90); break; case 'GIF': imageGif($dest_img, $dest, 90); break; case 'PNG': imagePng($dest_img, $dest, 90); break; case 'BMP': imageWBmp($dest_img, $dest, 90); break; } // free memory imageDestroy($src_img); ImageDestroy($dest_img); }
function makeImageGif($image, $w, $h, $dither, $save_image = '') { // Check dither mode if ($dither) { $dither = TRUE; } else { $dither = FALSE; } // Create a new true color image because we don't want to ruin the original $im_gif = @imagecreatetruecolor($w, $h) or debugMode(__LINE__, $php_errormsg); // Copy the original image into the new one @imagecopy($im_gif, $image, 0, 0, 0, 0, $w, $h) or debugMode(__LINE__, $php_errormsg); // Convert the new image to palette mode @imagetruecolortopalette($im_gif, $dither, 256) or debugMode(__LINE__, $php_errormsg); // Check if this needs to be saved if (empty($save_image)) { @imageGif($im_gif) or debugMode(__LINE__, $php_errormsg); } else { @imageGif($im_gif, $save_image) or debugMode(__LINE__, $php_errormsg); } // Destroy palette image @imageDestroy($im_gif) or debugMode(__LINE__, $php_errormsg); }
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; }
} if ($image_width == $orig_width && $image_height == $orig_height) { $imgNew = $img; } else { $imgNew = imageCreateTrueColor($image_width, $image_height); imagecopyresampled($imgNew, $img, 0, 0, 0, 0, $image_width, $image_height, imageSX($img), imageSY($img)); } } if (!file_exists($cache_folder_path)) { mkdir($cache_folder_path); } switch ($ext) { case "gif": header('Content-type: image/gif'); imageGif($imgNew, $cache_path); imageGif($imgNew); break; case "jpg": header('Content-type: image/jpeg'); imageJpeg($imgNew, $cache_path); imageJpeg($imgNew); break; case "jpeg": header('Content-type: image/jpeg'); imageJpeg($imgNew, $cache_path); imageJpeg($imgNew); break; case "png": header('Content-type: image/png'); imagePng($imgNew, $cache_path); imagePng($imgNew);
/** * Compressing a GIF file if not already LZW compressed. * This function is a workaround for the fact that ImageMagick and/or GD does not compress GIF-files to their minimun size (that is RLE or no compression used) * * The function takes a file-reference, $theFile, and saves it again through GD or ImageMagick in order to compress the file * GIF: * If $type is not set, the compression is done with ImageMagick (provided that $GLOBALS['TYPO3_CONF_VARS']['GFX']['im_path_lzw'] is pointing to the path of a lzw-enabled version of 'convert') else with GD (should be RLE-enabled!) * If $type is set to either 'IM' or 'GD' the compression is done with ImageMagick and GD respectively * PNG: * No changes. * * $theFile is expected to be a valid GIF-file! * The function returns a code for the operation. * * @param string $theFile Filepath * @param string $type See description of function * @return string Returns "GD" if GD was used, otherwise "IM" if ImageMagick was used. If nothing done at all, it returns empty string. */ public static function gif_compress($theFile, $type) { $gfxConf = $GLOBALS['TYPO3_CONF_VARS']['GFX']; $returnCode = ''; // GIF... if ($gfxConf['gif_compress'] && strtolower(substr($theFile, -4, 4)) == '.gif') { // IM if (($type == 'IM' || !$type) && $gfxConf['im'] && $gfxConf['im_path_lzw']) { // Use temporary file to prevent problems with read and write lock on same file on network file systems $temporaryName = dirname($theFile) . '/' . md5(uniqid('', TRUE)) . '.gif'; // Rename could fail, if a simultaneous thread is currently working on the same thing if (@rename($theFile, $temporaryName)) { $cmd = self::imageMagickCommand('convert', '"' . $temporaryName . '" "' . $theFile . '"', $gfxConf['im_path_lzw']); \TYPO3\CMS\Core\Utility\CommandUtility::exec($cmd); unlink($temporaryName); } $returnCode = 'IM'; if (@is_file($theFile)) { self::fixPermissions($theFile); } } elseif (($type == 'GD' || !$type) && $gfxConf['gdlib'] && !$gfxConf['gdlib_png']) { // GD $tempImage = imageCreateFromGif($theFile); imageGif($tempImage, $theFile); imageDestroy($tempImage); $returnCode = 'GD'; if (@is_file($theFile)) { self::fixPermissions($theFile); } } } return $returnCode; }
function show() { $this->ripper(); $split = split( "\.", basename( trim( $this->sButton ) ) ); $ext = $split[ count($split) - 1 ] ; switch( strtolower( $ext ) ) { case "gif" : Header( "Content-Type: image/gif" ); imageGif( $this->hNew ) ; break; case "png" : Header("Content-type: image/png"); ImagePNG($this->hNew); break; case "jpg" : case "jpeg" : Header("Content-type: image/jpeg"); ImageJPEG($this->hNew ); break; } $this->cleanup(); }