function save($save) { /* change ImageCreateTrueColor to ImageCreate if your GD not supported ImageCreateTrueColor function*/ $this->img["des"] = ImageCreateTrueColor($this->img["lebar_thumb"], $this->img["tinggi_thumb"]); @imagecopyresized($this->img["des"], $this->img["src"], 0, 0, 0, 0, $this->img["lebar_thumb"], $this->img["tinggi_thumb"], $this->img["lebar"], $this->img["tinggi"]); if ($this->img["format"] == "JPG" || $this->img["format"] == "JPEG") { //JPEG imageJPEG($this->img["des"], "{$save}", $this->img["quality"]); } elseif ($this->img["format"] == "PNG") { //PNG imagePNG($this->img["des"], "{$save}"); } elseif ($this->img["format"] == "GIF") { //GIF imageGIF($this->img["des"], "{$save}"); } elseif ($this->img["format"] == "WBMP") { //WBMP imageWBMP($this->img["des"], "{$save}"); } }
public function saveImg($path) { // Resize if ($this->resize) { $this->output = ImageCreateTrueColor($this->xOutput, $this->yOutput); ImageCopyResampled($this->output, $this->input, 0, 0, 0, 0, $this->xOutput, $this->yOutput, $this->xInput, $this->yInput); } // Save JPEG if ($this->format == "JPG" or $this->format == "JPEG") { if ($this->resize) { imageJPEG($this->output, $path, $this->quality); } else { copy($this->src, $path); } } elseif ($this->format == "PNG") { if ($this->resize) { imagePNG($this->output, $path); } else { copy($this->src, $path); } } elseif ($this->format == "GIF") { if ($this->resize) { imageGIF($this->output, $path); } else { copy($this->src, $path); } } }
/** * Write the image after being processed * * @param Asido_TMP &$tmp * @return boolean * @access protected */ function __write(&$tmp) { // try to guess format from extension // if (!$tmp->save) { $p = pathinfo($tmp->target_filename); ($tmp->save = $this->__mime_metaphone[metaphone($p['extension'])]) || ($tmp->save = $this->__mime_soundex[soundex($p['extension'])]); } $result = false; $imgContent = null; switch ($tmp->save) { case 'image/gif': imageTrueColorToPalette($tmp->target, true, 256); ob_start(); $result = @imageGIF($tmp->target); $imgContent = ob_get_clean(); break; case 'image/jpeg': ob_start(); $result = @imageJPEG($tmp->target, null, ASIDO_GD_JPEG_QUALITY); $imgContent = ob_get_clean(); break; case 'image/wbmp': ob_start(); $result = @imageWBMP($tmp->target); $imgContent = ob_get_clean(); break; default: case 'image/png': imageSaveAlpha($tmp->target, true); imageAlphaBlending($tmp->target, false); ob_start(); $result = @imagePNG($tmp->target, null, ASIDO_GD_PNG_QUALITY); $imgContent = ob_get_clean(); break; } if ($result) { jimport('joomla.filesystem.file'); JFile::write($tmp->target_filename, $imgContent); } @$this->__destroy_source($tmp); @$this->__destroy_target($tmp); return $result; }
/** * Creates a new image given an original path, a new path, a target width and height. * Optionally crops image to exactly match given width and height. * @params string $originalPath, string $newpath, int $width, int $height, bool $crop * @return void */ public function create($originalPath, $newPath, $width, $height, $crop = false) { // first, we grab the original image. We shouldn't ever get to this function unless the image is valid $imageSize = @getimagesize($originalPath); $oWidth = $imageSize[0]; $oHeight = $imageSize[1]; $finalWidth = 0; //For cropping, this is really "scale to width before chopping extra height" $finalHeight = 0; //For cropping, this is really "scale to height before chopping extra width" $do_crop_x = false; $do_crop_y = false; $crop_src_x = 0; $crop_src_y = 0; // first, if what we're uploading is actually smaller than width and height, we do nothing if ($oWidth < $width && $oHeight < $height) { $finalWidth = $oWidth; $finalHeight = $oHeight; $width = $oWidth; $height = $oHeight; } else { if ($crop && ($height >= $oHeight && $width <= $oWidth)) { //crop to width only -- don't scale anything $finalWidth = $oWidth; $finalHeight = $oHeight; $height = $oHeight; $do_crop_x = true; } else { if ($crop && ($width >= $oWidth && $height <= $oHeight)) { //crop to height only -- don't scale anything $finalHeight = $oHeight; $finalWidth = $oWidth; $width = $oWidth; $do_crop_y = true; } else { // otherwise, we do some complicated stuff // first, we divide original width and height by new width and height, and find which difference is greater $wDiff = $oWidth / $width; $hDiff = $oHeight / $height; if (!$crop && $wDiff > $hDiff) { //no cropping, just resize down based on target width $finalWidth = $width; $finalHeight = $oHeight / $wDiff; } else { if (!$crop) { //no cropping, just resize down based on target height $finalWidth = $oWidth / $hDiff; $finalHeight = $height; } else { if ($crop && $wDiff > $hDiff) { //resize down to target height, THEN crop off extra width $finalWidth = $oWidth / $hDiff; $finalHeight = $height; $do_crop_x = true; } else { if ($crop) { //resize down to target width, THEN crop off extra height $finalWidth = $width; $finalHeight = $oHeight / $wDiff; $do_crop_y = true; } } } } } } } //Calculate cropping to center image if ($do_crop_x) { /* //Get half the difference between scaled width and target width, // and crop by starting the copy that many pixels over from the left side of the source (scaled) image. $nudge = ($width / 10); //I have *no* idea why the width isn't centering exactly -- this seems to fix it though. $crop_src_x = ($finalWidth / 2.00) - ($width / 2.00) + $nudge; */ $crop_src_x = round(($oWidth - $width * $oHeight / $height) * 0.5); } if ($do_crop_y) { /* //Calculate cropping... //Get half the difference between scaled height and target height, // and crop by starting the copy that many pixels down from the top of the source (scaled) image. $crop_src_y = ($finalHeight / 2.00) - ($height / 2.00); */ $crop_src_y = round(($oHeight - $height * $oWidth / $width) * 0.5); } //create "canvas" to put new resized and/or cropped image into if ($crop) { $image = @imageCreateTrueColor($width, $height); } else { $image = @imageCreateTrueColor($finalWidth, $finalHeight); } switch ($imageSize[2]) { case IMAGETYPE_GIF: $im = @imageCreateFromGIF($originalPath); break; case IMAGETYPE_JPEG: $im = @imageCreateFromJPEG($originalPath); break; case IMAGETYPE_PNG: $im = @imageCreateFromPNG($originalPath); break; } if ($im) { // Better transparency - thanks for the ideas and some code from mediumexposure.com if ($imageSize[2] == IMAGETYPE_GIF || $imageSize[2] == IMAGETYPE_PNG) { $trnprt_indx = imagecolortransparent($im); // If we have a specific transparent color if ($trnprt_indx >= 0) { // Get the original image's transparent color's RGB values $trnprt_color = imagecolorsforindex($im, $trnprt_indx); // Allocate the same color in the new image resource $trnprt_indx = imagecolorallocate($image, $trnprt_color['red'], $trnprt_color['green'], $trnprt_color['blue']); // Completely fill the background of the new image with allocated color. imagefill($image, 0, 0, $trnprt_indx); // Set the background color for new image to transparent imagecolortransparent($image, $trnprt_indx); } else { if ($imageSize[2] == IMAGETYPE_PNG) { // Turn off transparency blending (temporarily) imagealphablending($image, false); // Create a new transparent color for image $color = imagecolorallocatealpha($image, 0, 0, 0, 127); // Completely fill the background of the new image with allocated color. imagefill($image, 0, 0, $color); // Restore transparency blending imagesavealpha($image, true); } } } $res = @imageCopyResampled($image, $im, 0, 0, $crop_src_x, $crop_src_y, $finalWidth, $finalHeight, $oWidth, $oHeight); if ($res) { switch ($imageSize[2]) { case IMAGETYPE_GIF: $res2 = imageGIF($image, $newPath); break; case IMAGETYPE_JPEG: $compression = defined('AL_THUMBNAIL_JPEG_COMPRESSION') ? AL_THUMBNAIL_JPEG_COMPRESSION : 80; $res2 = imageJPEG($image, $newPath, $compression); break; case IMAGETYPE_PNG: $res2 = imagePNG($image, $newPath); break; } } } }
/** * Saves the image to a given filename, if no filename is given then a default is created. * * @param string $save The new image filename. */ public function save($save = "") { //save thumb if (empty($save)) { $save = strtolower("./thumb." . $this->image["outputformat"]); } else { $this->image["outputformat"] = preg_replace("/.*\\.(.*)\$/", "\\1", $save); //$this->image["outputformat"] = preg_replace(".*\.(.*)$", "\\1", $save); $this->image["outputformat"] = strtoupper($this->image["outputformat"]); } $this->createResampledImage(); if ($this->image["outputformat"] == "JPG" || $this->image["outputformat"] == "JPEG") { //JPEG imageJPEG($this->image["des"], $save, $this->image["quality"]); } elseif ($this->image["outputformat"] == "PNG") { //PNG imagePNG($this->image["des"], $save, 0); } elseif ($this->image["outputformat"] == "GIF") { //GIF imageGIF($this->image["des"], $save); } elseif ($this->image["outputformat"] == "WBMP") { //WBMP imageWBMP($this->image["des"], $save); } }
function convertImage($type) { /* check the converted image type availability, if it is not available, it will be casted to jpeg :) */ $validtype = $this->validateType($type); if ($this->output) { /* show the image */ switch ($validtype) { case 'jpeg': // Added jpe // Added jpe case 'jpe': case 'jpg': header("Content-type: image/jpeg"); if ($this->imtype == 'gif' or $this->imtype == 'png') { $image = $this->replaceTransparentWhite($this->im); @imageJPEG($image); } else { @imageJPEG($this->im); } break; case 'gif': header("Content-type: image/gif"); @imageGIF($this->im); break; case 'png': header("Content-type: image/png"); @imagePNG($this->im); break; case 'wbmp': header("Content-type: image/vnd.wap.wbmp"); @imageWBMP($this->im); break; case 'swf': header("Content-type: application/x-shockwave-flash"); $this->imageSWF($this->im); break; } } else { // Added Support vor different directory if (DEFINED('V_TEMP_DIR')) { $this->newimname = V_TEMP_DIR . $this->imname . '.' . $validtype; } else { $this->newimname = $this->imname . '.' . $validtype; } /* save the image */ switch ($validtype) { case 'jpeg': // Added jpe // Added jpe case 'jpe': case 'jpg': if ($this->imtype == 'gif' or $this->imtype == 'png') { /* replace transparent with white */ $image = $this->replaceTransparentWhite($this->im); @imageJPEG($image, $this->newimname); } else { @imageJPEG($this->im, $this->newimname); } break; case 'gif': @imageGIF($this->im, $this->newimname); break; case 'png': @imagePNG($this->im, $this->newimname); break; case 'wbmp': @imageWBMP($this->im, $this->newimname); break; case 'swf': $this->imageSWF($this->im, $this->newimname); break; } } }
/** * Write the image after being processed * * @param Asido_TMP &$tmp * @return boolean * @access protected */ function __write(&$tmp) { // try to guess format from extension // if (!$tmp->save) { $p = pathinfo($tmp->target_filename); ($tmp->save = $this->__mime_metaphone[metaphone($p['extension'])]) || ($tmp->save = $this->__mime_soundex[soundex($p['extension'])]); } $result = false; switch ($tmp->save) { case 'image/gif': imageTrueColorToPalette($tmp->target, true, 256); $result = @imageGIF($tmp->target, $tmp->target_filename); break; case 'image/jpeg': $result = @imageJPEG($tmp->target, $tmp->target_filename, ASIDO_GD_JPEG_QUALITY); break; case 'image/wbmp': $result = @imageWBMP($tmp->target, $tmp->target_filename); break; default: case 'image/png': imageSaveAlpha($tmp->target, true); imageAlphaBlending($tmp->target, false); $result = @imagePNG($tmp->target, $tmp->target_filename); break; } @$this->__destroy_source($tmp); @$this->__destroy_target($tmp); return $result; }
imageString($im, 5, $dx + $gxb + 2 * $ticW, $i - $labelH, sprintf("%3.1f", $wnd), $blue); //} $wnd -= $dwnd; if ($wnd < 0) { $wnd = -$wnd; } } /* draw the wind direction graph */ $scale = $gyb / 360; /* wind direction can only go from 0 to 360 degrees */ for ($i = 0; $i + 1 < $row_count; $i++) { $x1 = $i * $gxb / $row_count + $dx; $y1 = $gyb + $dy - $windd[$i] * $scale; $x2 = ($i + 1) * $gxb / $row_count + $dx; $y2 = $gyb + $dy - $windd[$i + 1] * $scale; imageLine($im, $x1, $y1, $x2, $y2, $red); } /* draw the wind speed graph */ $scale = $gyb / ($wnds_max - $wnds_min); for ($i = 0; $i + 1 < $row_count; $i++) { $x1 = $i * $gxb / $row_count + $dx; $y1 = $gyb + $dy - ($winds[$i] - $wnds_min) * $scale; $x2 = ($i + 1) * $gxb / $row_count + $dx; $y2 = $gyb + $dy - ($winds[$i + 1] - $wnds_min) * $scale; imageLine($im, $x1, $y1, $x2, $y2, $blue); } /* use gif as the image format */ header("Content-type: image/gif"); imageGIF($im); /* lastly delete the image from memory */ imageDestroy($im);
private function createNewImage($newImg, $newName, $imgInfo) { $this->path = rtrim($this->path, "/") . "/"; 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 $result = imagePng($newImg, $this->path . $newName); break; } imagedestroy($newImg); return $newName; }
/** * Creates a new image given an original path, a new path, a target width and height. * @params string $originalPath, string $newpath, int $width, int $height * @return void */ public function create($originalPath, $newPath, $width, $height) { // first, we grab the original image. We shouldn't ever get to this function unless the image is valid $imageSize = @getimagesize($originalPath); $oWidth = $imageSize[0]; $oHeight = $imageSize[1]; $finalWidth = 0; $finalHeight = 0; // first, if what we're uploading is actually smaller than width and height, we do nothing if ($oWidth < $width && $oHeight < $height) { $finalWidth = $oWidth; $finalHeight = $oHeight; } else { // otherwise, we do some complicated stuff // first, we divide original width and height by new width and height, and find which difference is greater $wDiff = $oWidth / $width; $hDiff = $oHeight / $height; if ($wDiff > $hDiff) { // there's more of a difference between width than height, so if we constrain to width, we should be safe $finalWidth = $width; $finalHeight = $oHeight / ($oWidth / $width); } else { // more of a difference in height, so we do the opposite $finalWidth = $oWidth / ($oHeight / $height); $finalHeight = $height; } } $image = @imageCreateTrueColor($finalWidth, $finalHeight); switch ($imageSize[2]) { case IMAGETYPE_GIF: $im = @imageCreateFromGIF($originalPath); break; case IMAGETYPE_JPEG: $im = @imageCreateFromJPEG($originalPath); break; case IMAGETYPE_PNG: $im = @imageCreateFromPNG($originalPath); break; } if ($im) { // Better transparency - thanks for the ideas and some code from mediumexposure.com if ($imageSize[2] == IMAGETYPE_GIF || $imageSize[2] == IMAGETYPE_PNG) { $trnprt_indx = imagecolortransparent($im); // If we have a specific transparent color if ($trnprt_indx >= 0) { // Get the original image's transparent color's RGB values $trnprt_color = imagecolorsforindex($im, $trnprt_indx); // Allocate the same color in the new image resource $trnprt_indx = imagecolorallocate($image, $trnprt_color['red'], $trnprt_color['green'], $trnprt_color['blue']); // Completely fill the background of the new image with allocated color. imagefill($image, 0, 0, $trnprt_indx); // Set the background color for new image to transparent imagecolortransparent($image, $trnprt_indx); } else { if ($imageSize[2] == IMAGETYPE_PNG) { // Turn off transparency blending (temporarily) imagealphablending($image, false); // Create a new transparent color for image $color = imagecolorallocatealpha($image, 0, 0, 0, 127); // Completely fill the background of the new image with allocated color. imagefill($image, 0, 0, $color); // Restore transparency blending imagesavealpha($image, true); } } } $res = @imageCopyResampled($image, $im, 0, 0, 0, 0, $finalWidth, $finalHeight, $oWidth, $oHeight); if ($res) { switch ($imageSize[2]) { case IMAGETYPE_GIF: $res2 = imageGIF($image, $newPath); break; case IMAGETYPE_JPEG: $res2 = imageJPEG($image, $newPath, AL_THUMBNAIL_JPEG_COMPRESSION); break; case IMAGETYPE_PNG: $res2 = imagePNG($image, $newPath); break; } } } }
/** * Creates a new image given an original path, a new path, a target width and height. * Optionally crops image to exactly match given width and height. * @param string $originalPath The path to the original image * @param string $newpath The path to the new image to be created * @param int $width The maximum width of the new image * @param int $height The maximum height of the new image * @param bool $crop = false Set to true to resize and crop the image, set to false to just resize the image * @return bool * @example Resizing from 200x200 to 100x50 with $crop = false will result in a 50 x 50 image (same aspect ratio as source, scaled down to a quarter of size) * @example Resizing from 200x200 to 100x50 with $crop = true will result in a 100 x 50 image (same aspect ratio as source, scaled down to a half of size and cropped in height) * @example Resizing from 200x200 to 1000x1000 with either $crop = false or $crop = true in a copy of the original image (200x200) */ public function create($originalPath, $newPath, $width, $height, $crop = false) { // first, we grab the original image. We shouldn't ever get to this function unless the image is valid $imageSize = @getimagesize($originalPath); if ($imageSize === false) { return false; } $oWidth = $imageSize[0]; $oHeight = $imageSize[1]; $finalWidth = 0; //For cropping, this is really "scale to width before chopping extra height" $finalHeight = 0; //For cropping, this is really "scale to height before chopping extra width" $do_crop_x = false; $do_crop_y = false; $crop_src_x = 0; $crop_src_y = 0; // first, if what we're uploading is actually smaller than width and height, we do nothing if ($oWidth < $width && $oHeight < $height) { $finalWidth = $oWidth; $finalHeight = $oHeight; $width = $oWidth; $height = $oHeight; } else { if ($crop && ($height >= $oHeight && $width <= $oWidth)) { //crop to width only -- don't scale anything $finalWidth = $oWidth; $finalHeight = $oHeight; $height = $oHeight; $do_crop_x = true; } else { if ($crop && ($width >= $oWidth && $height <= $oHeight)) { //crop to height only -- don't scale anything $finalHeight = $oHeight; $finalWidth = $oWidth; $width = $oWidth; $do_crop_y = true; } else { // otherwise, we do some complicated stuff // first, we divide original width and height by new width and height, and find which difference is greater $wDiff = $oWidth / $width; $hDiff = $height != 0 ? $oHeight / $height : 0; if (!$crop && $wDiff > $hDiff) { //no cropping, just resize down based on target width $finalWidth = $width; $finalHeight = $wDiff != 0 ? $oHeight / $wDiff : 0; } else { if (!$crop) { //no cropping, just resize down based on target height $finalWidth = $hDiff != 0 ? $oWidth / $hDiff : 0; $finalHeight = $height; } else { if ($crop && $wDiff > $hDiff) { //resize down to target height, THEN crop off extra width $finalWidth = $hDiff != 0 ? $oWidth / $hDiff : 0; $finalHeight = $height; $do_crop_x = true; } else { if ($crop) { //resize down to target width, THEN crop off extra height $finalWidth = $width; $finalHeight = $wDiff != 0 ? $oHeight / $wDiff : 0; $do_crop_y = true; } } } } } } } //Calculate cropping to center image if ($do_crop_x) { /* //Get half the difference between scaled width and target width, // and crop by starting the copy that many pixels over from the left side of the source (scaled) image. $nudge = ($width / 10); //I have *no* idea why the width isn't centering exactly -- this seems to fix it though. $crop_src_x = ($finalWidth / 2.00) - ($width / 2.00) + $nudge; */ $crop_src_x = round(($oWidth - $width * $oHeight / $height) * 0.5); } if ($do_crop_y) { /* //Calculate cropping... //Get half the difference between scaled height and target height, // and crop by starting the copy that many pixels down from the top of the source (scaled) image. $crop_src_y = ($finalHeight / 2.00) - ($height / 2.00); */ $crop_src_y = round(($oHeight - $height * $oWidth / $width) * 0.5); } $processed = false; if (class_exists('Imagick')) { try { $image = new Imagick(); $imageRead = false; if ($crop) { $image->setSize($finalWidth, $finalHeight); if ($image->readImage($originalPath) === true) { $image->cropThumbnailImage($width, $height); /* * Remove the canvas * See: http://php.net/manual/en/imagick.cropthumbnailimage.php#106710 */ $image->setImagePage(0, 0, 0, 0); $imageRead = true; } } else { $image->setSize($width, $height); if ($image->readImage($originalPath) === true) { $image->thumbnailImage($finalWidth, $finalHeight); $imageRead = true; } } if ($imageRead) { if ($image->getCompression() == imagick::COMPRESSION_JPEG) { $image->setCompressionQuality($this->jpegCompression); } if ($image->writeImage($newPath) === true) { $processed = true; } } } catch (Exception $x) { } } if (!$processed) { //create "canvas" to put new resized and/or cropped image into if ($crop) { $image = @imageCreateTrueColor($width, $height); } else { $image = @imageCreateTrueColor($finalWidth, $finalHeight); } if ($image === false) { return false; } $im = false; switch ($imageSize[2]) { case IMAGETYPE_GIF: $im = @imageCreateFromGIF($originalPath); break; case IMAGETYPE_JPEG: $im = @imageCreateFromJPEG($originalPath); break; case IMAGETYPE_PNG: $im = @imageCreateFromPNG($originalPath); break; default: @imagedestroy($image); return false; } if ($im === false) { @imagedestroy($image); return false; } // Better transparency - thanks for the ideas and some code from mediumexposure.com if ($imageSize[2] == IMAGETYPE_GIF || $imageSize[2] == IMAGETYPE_PNG) { $trnprt_indx = imagecolortransparent($im); // If we have a specific transparent color if ($trnprt_indx >= 0 && $trnprt_indx < imagecolorstotal($im)) { // Get the original image's transparent color's RGB values $trnprt_color = imagecolorsforindex($im, $trnprt_indx); // Allocate the same color in the new image resource $trnprt_indx = imagecolorallocate($image, $trnprt_color['red'], $trnprt_color['green'], $trnprt_color['blue']); // Completely fill the background of the new image with allocated color. imagefill($image, 0, 0, $trnprt_indx); // Set the background color for new image to transparent imagecolortransparent($image, $trnprt_indx); } else { if ($imageSize[2] == IMAGETYPE_PNG) { // Turn off transparency blending (temporarily) imagealphablending($image, false); // Create a new transparent color for image $color = imagecolorallocatealpha($image, 0, 0, 0, 127); // Completely fill the background of the new image with allocated color. imagefill($image, 0, 0, $color); // Restore transparency blending imagesavealpha($image, true); } } } $res = @imageCopyResampled($image, $im, 0, 0, $crop_src_x, $crop_src_y, $finalWidth, $finalHeight, $oWidth, $oHeight); @imagedestroy($im); if ($res === false) { @imagedestroy($image); return false; } $res2 = false; switch ($imageSize[2]) { case IMAGETYPE_GIF: $res2 = @imageGIF($image, $newPath); break; case IMAGETYPE_JPEG: $res2 = @imageJPEG($image, $newPath, $this->jpegCompression); break; case IMAGETYPE_PNG: $res2 = @imagePNG($image, $newPath); break; } @imagedestroy($image); if ($res2 === false) { return false; } } @chmod($newPath, FILE_PERMISSIONS_MODE); return true; }
function generateImage($save = '', $show = true) { if ($this->img['format'] == 'GIF' && !$this->gifsupport) { // --- kein caching -> gif ausgeben $this->send(); } $this->resampleImage(); $this->applyFilters(); if ($this->img['format'] == 'JPG' || $this->img['format'] == 'JPEG') { imageJPEG($this->img['des'], $save, $this->img['quality']); } elseif ($this->img['format'] == 'PNG') { imagePNG($this->img['des'], $save); } elseif ($this->img['format'] == 'GIF') { imageGIF($this->img['des'], $save); } elseif ($this->img['format'] == 'WBMP') { imageWBMP($this->img['des'], $save); } if ($show) { $this->send($save); } }
function show() { if ($this->img['format'] == "JPG" || $this->img['format'] == "JPEG") { //JPEG imageJPEG($this->img['src'], "", $this->img['quality']); } elseif ($this->img['format'] == "PNG") { //PNG imagePNG($this->img['src']); } elseif ($this->img['format'] == "GIF") { //GIF imageGIF($this->img['src']); } imagedestroy($this->img['src']); }
function generateThumbnail($w, $h, $dstPath) { $cw = $this->width(); $ch = $this->height(); $format = $this->format(); $image = $this->image(); if ($cw > $w) { $new_width = $w; $new_height = round($ch * ($new_width * 100 / $cw) / 100); if ($new_height > $h) { $new_height_before = $new_height; $new_height = $h; $new_width = round($new_width * ($new_height * 100 / $new_height_before) / 100); } } else { if ($ch > $h) { $new_height = $h; $new_width = round($cw * ($new_height * 100 / $ch) / 100); if ($new_width > $w) { $new_width_before = $new_width; $new_width = $w; $new_height = round($new_height * ($new_width * 100 / $new_width_before) / 100); } } else { $new_width = $w; $new_height = round($ch * ($new_width * 100 / $cw) / 100); if ($new_height > $h) { $new_height_before = $new_height; $new_height = $h; $new_width = round($new_width * ($new_height * 100 / $new_height_before) / 100); } } } if (function_exists('ImageCreateTrueColor')) { $new_image = ImageCreateTrueColor($new_width, $new_height); } else { $new_image = ImageCreate($new_width, $new_height); } if (function_exists('imagecopyresampled')) { if ($format == 'png' || $format == 'gif') { imagecolortransparent($new_image, imagecolorallocatealpha($new_image, 0, 0, 0, 127)); } else { imagecolortransparent($new_image, imagecolorallocate($new_image, 0, 0, 0)); } imagealphablending($new_image, false); imagesavealpha($new_image, true); @imagecopyresampled($new_image, $image, 0, 0, 0, 0, $new_width, $new_height, $cw, $ch); } else { @imagecopyresized($new_image, $image, 0, 0, 0, 0, $new_width, $new_height, $cw, $ch); } switch ($format) { case 'jpg': imageJPEG($new_image, $dstPath, 750); break; case 'png': imagePNG($new_image, $dstPath); break; case 'gif': imageGIF($new_image, $dstPath); break; default: throw new Exception('Unknown image format'); break; } }
protected function _output($method, $format, $filename) { switch ($format) { case parent::FORMAT_GIF: if ($method == parent::OUTPUT_INLINE || $method == parent::OUTPUT_DOWNLOAD) { return imageGIF($this->canvas); } else { if ($method == parent::OUTPUT_SAVE) { return imageGIF($this->canvas, $filename); } } break; case parent::FORMAT_JPEG: if ($method == parent::OUTPUT_INLINE || $method == parent::OUTPUT_DOWNLOAD) { return imageJPEG($this->canvas, NULL, $this->quality); } else { if ($method == parent::OUTPUT_SAVE) { return imageJPEG($this->canvas, $filename, $this->quality); } } break; case parent::FORMAT_PNG: if ($method == parent::OUTPUT_INLINE || $method == parent::OUTPUT_DOWNLOAD) { return imagePNG($this->canvas); } else { if ($method == parent::OUTPUT_SAVE) { return imagePNG($this->canvas, $filename); } } break; } return false; // The output method or format is missing! }
public function edit($path, $crop_x, $crop_y, $crop_w, $crop_h, $target_w, $target_h) { $imageSize = @getimagesize($path); $img_type = $imageSize[2]; //create "canvas" to put new resized and/or cropped image into $image = @imageCreateTrueColor($target_w, $target_h); switch($img_type) { case IMAGETYPE_GIF: $im = @imageCreateFromGIF($path); break; case IMAGETYPE_JPEG: $im = @imageCreateFromJPEG($path); break; case IMAGETYPE_PNG: $im = @imageCreateFromPNG($path); break; } if ($im) { // Better transparency - thanks for the ideas and some code from mediumexposure.com if (($img_type == IMAGETYPE_GIF) || ($img_type == IMAGETYPE_PNG)) { $trnprt_indx = imagecolortransparent($im); // If we have a specific transparent color if ($trnprt_indx >= 0) { // Get the original image's transparent color's RGB values $trnprt_color = imagecolorsforindex($im, $trnprt_indx); // Allocate the same color in the new image resource $trnprt_indx = imagecolorallocate($image, $trnprt_color['red'], $trnprt_color['green'], $trnprt_color['blue']); // Completely fill the background of the new image with allocated color. imagefill($image, 0, 0, $trnprt_indx); // Set the background color for new image to transparent imagecolortransparent($image, $trnprt_indx); } else if ($img_type == IMAGETYPE_PNG) { // Turn off transparency blending (temporarily) imagealphablending($image, false); // Create a new transparent color for image $color = imagecolorallocatealpha($image, 0, 0, 0, 127); // Completely fill the background of the new image with allocated color. imagefill($image, 0, 0, $color); // Restore transparency blending imagesavealpha($image, true); } } $res = @imageCopyResampled($image, $im, 0, 0, $crop_x, $crop_y, $target_w, $target_h, $crop_w, $crop_h); if ($res) { switch($img_type) { case IMAGETYPE_GIF: $res2 = imageGIF($image, $path); break; case IMAGETYPE_JPEG: $compression = defined('AL_THUMBNAIL_JPEG_COMPRESSION') ? AL_THUMBNAIL_JPEG_COMPRESSION : 80; $res2 = imageJPEG($image, $path, $compression); break; case IMAGETYPE_PNG: $res2 = imagePNG($image, $path); break; } } } }
function makewatermark($srcfile) { global $_SCONFIG; if ($_SCONFIG['watermark'] && function_exists('imageCreateFromJPEG') && function_exists('imageCreateFromPNG') && function_exists('imageCopyMerge')) { $srcfile = A_DIR . '/' . $srcfile; $watermark_file = $_SCONFIG['watermarkfile']; $watermarkstatus = $_SCONFIG['watermarkstatus']; $fileext = fileext($watermark_file); $ispng = $fileext == 'png' ? true : false; $attachinfo = @getimagesize($srcfile); if (!empty($attachinfo) && is_array($attachinfo) && $attachinfo[2] != 1 && $attachinfo['mime'] != 'image/gif') { } else { return ''; } $watermark_logo = $ispng ? @imageCreateFromPNG($watermark_file) : @imageCreateFromGIF($watermark_file); if (!$watermark_logo) { return ''; } $logo_w = imageSX($watermark_logo); $logo_h = imageSY($watermark_logo); $img_w = $attachinfo[0]; $img_h = $attachinfo[1]; $wmwidth = $img_w - $logo_w; $wmheight = $img_h - $logo_h; if (is_readable($watermark_file) && $wmwidth > 100 && $wmheight > 100) { switch ($attachinfo['mime']) { case 'image/jpeg': $dst_photo = imageCreateFromJPEG($srcfile); break; case 'image/gif': $dst_photo = imageCreateFromGIF($srcfile); break; case 'image/png': $dst_photo = imageCreateFromPNG($srcfile); break; default: break; } switch ($watermarkstatus) { case 1: $x = +5; $y = +5; break; case 2: $x = ($img_w - $logo_w) / 2; $y = +5; break; case 3: $x = $img_w - $logo_w - 5; $y = +5; break; case 4: $x = +5; $y = ($img_h - $logo_h) / 2; break; case 5: $x = ($img_w - $logo_w) / 2; $y = ($img_h - $logo_h) / 2; break; case 6: $x = $img_w - $logo_w - 5; $y = ($img_h - $logo_h) / 2; break; case 7: $x = +5; $y = $img_h - $logo_h - 5; break; case 8: $x = ($img_w - $logo_w) / 2; $y = $img_h - $logo_h - 5; break; case 9: $x = $img_w - $logo_w - 5; $y = $img_h - $logo_h - 5; break; } if ($ispng) { $watermark_photo = imagecreatetruecolor($img_w, $img_h); imageCopy($watermark_photo, $dst_photo, 0, 0, 0, 0, $img_w, $img_h); imageCopy($watermark_photo, $watermark_logo, $x, $y, 0, 0, $logo_w, $logo_h); $dst_photo = $watermark_photo; } else { imageAlphaBlending($watermark_logo, true); imageCopyMerge($dst_photo, $watermark_logo, $x, $y, 0, 0, $logo_w, $logo_h, $_SCONFIG['watermarktrans']); } switch ($attachinfo['mime']) { case 'image/jpeg': imageJPEG($dst_photo, $srcfile, $_SCONFIG['watermarkjpgquality']); break; case 'image/gif': imageGIF($dst_photo, $srcfile); break; case 'image/png': imagePNG($dst_photo, $srcfile); break; } } } }
public function create($originalPath, $newPath, $newWidth, $newHeight) { $originalImageData = @getimagesize($originalPath); $originalWidth = $originalImageData[0]; $originalHeight = $originalImageData[1]; if ($newWidth >= $originalWidth && $newHeight >= $originalHeight) { //new width+height is BIGGER than original width+height -- do not scale or crop. $scaleToWidth = $originalWidth; $scaleToHeight = $originalHeight; $newWidth = $originalWidth; $newHeight = $originalHeight; $cropWidth = false; $cropHeight = false; } else { if ($newHeight >= $originalHeight && $newWidth <= $originalWidth) { //crop to width only -- don't scale anything $scaleToWidth = $originalWidth; $scaleToHeight = $originalHeight; $newHeight = $originalHeight; $cropWidth = true; $cropHeight = false; } else { if ($newWidth >= $originalWidth && $newHeight <= $originalHeight) { //crop to height only -- don't scale anything $scaleToHeight = $originalHeight; $scaleToWidth = $originalWidth; $newWidth = $originalWidth; $cropWidth = false; $cropHeight = true; } else { //Scale down until we hit one of the new dimensions, then crop the other dimension. $widthRatio = $originalWidth / $newWidth; $heightRatio = $originalHeight / $newHeight; if ($widthRatio < $heightRatio) { //we'll scale to width's proportion, then crop height to target $scaleToWidth = $newWidth; $scaleToHeight = $originalHeight / $widthRatio; $cropWidth = false; $cropHeight = true; } else { //we'll scale to height's proportion, then crop width to target $scaleToWidth = $originalWidth / $heightRatio; $scaleToHeight = $newHeight; $cropWidth = true; $cropHeight = false; } } } } $newImage = @imageCreateTrueColor($newWidth, $newHeight); $imageType = $originalImageData[2]; switch ($imageType) { case IMAGETYPE_GIF: $im = @imageCreateFromGIF($originalPath); break; case IMAGETYPE_JPEG: $im = @imageCreateFromJPEG($originalPath); break; case IMAGETYPE_PNG: $im = @imageCreateFromPNG($originalPath); break; } if ($im) { // Better transparency - thanks for the ideas and some code from mediumexposure.com if ($imageType == IMAGETYPE_GIF || $imageType == IMAGETYPE_PNG) { $trnprt_indx = imagecolortransparent($im); // If we have a specific transparent color if ($trnprt_indx >= 0) { // Get the original image's transparent color's RGB values $trnprt_color = imagecolorsforindex($im, $trnprt_indx); // Allocate the same color in the new image resource $trnprt_indx = imagecolorallocate($newImage, $trnprt_color['red'], $trnprt_color['green'], $trnprt_color['blue']); // Completely fill the background of the new image with allocated color. imagefill($newImage, 0, 0, $trnprt_indx); // Set the background color for new image to transparent imagecolortransparent($newImage, $trnprt_indx); } else { if ($imageType == IMAGETYPE_PNG) { // Turn off transparency blending (temporarily) imagealphablending($newImage, false); // Create a new transparent color for image $color = imagecolorallocatealpha($newImage, 0, 0, 0, 127); // Completely fill the background of the new image with allocated color. imagefill($newImage, 0, 0, $color); // Restore transparency blending imagesavealpha($newImage, true); } } } //FIGURE OUT CROP DIMENSIONS... $src_x = 0; $src_y = 0; //Calculate cropping to center image if ($cropWidth) { $src_x = round(($originalWidth - $newWidth * $originalHeight / $newHeight) * 0.5); } if ($cropHeight) { $src_y = round(($originalHeight - $newHeight * $originalWidth / $newWidth) * 0.5); } //SCALE/CROP: $res = @imageCopyResampled($newImage, $im, 0, 0, $src_x, $src_y, $scaleToWidth, $scaleToHeight, $originalWidth, $originalHeight); if ($res) { switch ($imageType) { case IMAGETYPE_GIF: imageGIF($newImage, $newPath); break; case IMAGETYPE_JPEG: $compression = defined('AL_THUMBNAIL_JPEG_COMPRESSION') ? AL_THUMBNAIL_JPEG_COMPRESSION : 80; //Concrete < 5.4.1 didn't have this constant imageJPEG($newImage, $newPath, $compression); break; case IMAGETYPE_PNG: imagePNG($newImage, $newPath); break; } } } }
/** * Appends information about the source image to the thumbnail. * * @param string $thumbnail * @return string */ protected function appendSourceInfo($thumbnail) { if (!function_exists('imageCreateFromString') || !function_exists('imageCreateTrueColor')) { return $thumbnail; } $imageSrc = imageCreateFromString($thumbnail); // get image size $width = imageSX($imageSrc); $height = imageSY($imageSrc); // increase height $heightDst = $height + self::$sourceInfoLineHeight * 2; // create new image $imageDst = imageCreateTrueColor($width, $heightDst); imageAlphaBlending($imageDst, false); // set background color $background = imageColorAllocate($imageDst, 102, 102, 102); imageFill($imageDst, 0, 0, $background); // copy image imageCopy($imageDst, $imageSrc, 0, 0, 0, 0, $width, $height); imageSaveAlpha($imageDst, true); // get font size $font = 2; $fontWidth = imageFontWidth($font); $fontHeight = imageFontHeight($font); $fontColor = imageColorAllocate($imageDst, 255, 255, 255); // write source info $line1 = $this->sourceName; // imageString supports only ISO-8859-1 encoded strings if (CHARSET != 'ISO-8859-1') { $line1 = StringUtil::convertEncoding(CHARSET, 'ISO-8859-1', $line1); } // truncate text if necessary $maxChars = floor($width / $fontWidth); if (strlen($line1) > $maxChars) { $line1 = $this->truncateSourceName($line1, $maxChars); } $line2 = $this->sourceWidth . 'x' . $this->sourceHeight . ' ' . FileUtil::formatFilesize($this->sourceSize); // write line 1 // calculate text position $textX = 0; $textY = 0; if ($fontHeight < self::$sourceInfoLineHeight) { $textY = intval(round((self::$sourceInfoLineHeight - $fontHeight) / 2)); } if (strlen($line1) * $fontWidth < $width) { $textX = intval(round(($width - strlen($line1) * $fontWidth) / 2)); } imageString($imageDst, $font, $textX, $height + $textY, $line1, $fontColor); // write line 2 // calculate text position $textX = 0; $textY = 0; if ($fontHeight < self::$sourceInfoLineHeight) { $textY = self::$sourceInfoLineHeight + intval(round((self::$sourceInfoLineHeight - $fontHeight) / 2)); } if (strlen($line2) * $fontWidth < $width) { $textX = intval(round(($width - strlen($line2) * $fontWidth) / 2)); } imageString($imageDst, $font, $textX, $height + $textY, $line2, $fontColor); // output image ob_start(); if ($this->imageType == 1 && function_exists('imageGIF')) { @imageGIF($imageDst); $this->mimeType = 'image/gif'; } else { if (($this->imageType == 1 || $this->imageType == 3) && function_exists('imagePNG')) { @imagePNG($imageDst); $this->mimeType = 'image/png'; } else { if (function_exists('imageJPEG')) { @imageJPEG($imageDst, null, 90); $this->mimeType = 'image/jpeg'; } else { return false; } } } @imageDestroy($imageDst); $thumbnail = ob_get_contents(); ob_end_clean(); return $thumbnail; }
function _createThumb($filename) { $filepath = $this->directory . $filename; list($width, $height, $type) = getimagesize($filepath); switch (strtolower($type)) { case 1: $source = imagecreatefromgif($filepath); break; case 2: $source = imagecreatefromjpeg($filepath); break; case 3: $source = imagecreatefrompng($filepath); break; default: return; } $thumb = imagecreatetruecolor($this->thumbWidth, $this->thumbHeight); imagecopyresized($thumb, $source, 0, 0, 0, 0, $this->thumbWidth, $this->thumbHeight, $width, $height); $thumbfilename = $this->directory . $this->thumbPrefix . $filename; switch (strtolower($type)) { case 1: imageGIF($thumb, $thumbfilename); case 2: imageJPEG($thumb, $thumbfilename); case 3: imagePNG($thumb, $thumbfilename); } unset($source); unset($thumb); }
/** * function imageCreate (resource $thumb) * * Create a new image from the $thumb resource * * return booelan */ function imageCreate($thumb) { if ($this->data[2] == 1) { $ok = imageGIF($thumb, $this->target, $this->quality); } elseif ($this->data[2] == 3) { $quality = 10 - round($this->quality / 10); $quality = $quality < 0 ? 0 : ($quality > 9 ? 9 : $quality); $ok = imagePNG($thumb, $this->target, $quality); } else { $ok = imageJPEG($thumb, $this->target, $this->quality); } if ($ok) { imageDestroy($thumb); return true; } else { return false; } }
/** * Saves the image to a given filename, if no filename is given then a default is created. * * @param string $save The new image filename. */ public function save($save = "") { //save thumb if (empty($save)) { $save = strtolower("./thumb." . $this->image["format"]); } $this->createResampledImage(); if ($this->image["format"] == "JPG" || $this->image["format"] == "JPEG") { //JPEG imageJPEG($this->image["des"], $save, $this->image["quality"]); } elseif ($this->image["format"] == "PNG") { //PNG imagePNG($this->image["des"], $save); } elseif ($this->image["format"] == "GIF") { //GIF imageGIF($this->image["des"], $save); } elseif ($this->image["format"] == "WBMP") { //WBMP imageWBMP($this->image["des"], $save); } }
function output() { if (ALLOC_GD_IMAGE_TYPE == "PNG") { header("Content-type: image/png"); imagePNG($this->image); } else { header("Content-type: image/gif"); imageGIF($this->image); } }
function generateImage($file = '', $show = true) { global $REX; if ($this->img['format'] == 'GIF' && !$this->gifsupport) { // --- kein caching -> gif ausgeben $this->send(); } $this->resampleImage(); $this->applyFilters(); $this->checkCacheFiles(); if ($this->img['format'] == 'JPG' || $this->img['format'] == 'JPEG') { imageJPEG($this->img['des'], $file, $this->img['quality']); } elseif ($this->img['format'] == 'PNG') { imagePNG($this->img['des'], $file); } elseif ($this->img['format'] == 'GIF') { imageGIF($this->img['des'], $file); } elseif ($this->img['format'] == 'WBMP') { imageWBMP($this->img['des'], $file); } chmod($file, $REX['FILEPERM']); if ($show) { $this->send($file); } }
function watermark($target, $watermark_file, $ext, $watermarkstatus = 9, $watermarktrans = 50) { $gdsurporttype = array(); if (function_exists('imageAlphaBlending') && function_exists('getimagesize')) { if (function_exists('imageGIF')) { $gdsurporttype[] = 'gif'; } if (function_exists('imagePNG')) { $gdsurporttype[] = 'png'; } if (function_exists('imageJPEG')) { $gdsurporttype[] = 'jpg'; $gdsurporttype[] = 'jpeg'; } } if ($gdsurporttype && in_array($ext, $gdsurporttype)) { $attachinfo = getimagesize($target); $watermark_logo = imageCreateFromGIF($watermark_file); $logo_w = imageSX($watermark_logo); $logo_h = imageSY($watermark_logo); $img_w = $attachinfo[0]; $img_h = $attachinfo[1]; $wmwidth = $img_w - $logo_w; $wmheight = $img_h - $logo_h; $animatedgif = 0; if ($attachinfo['mime'] == 'image/gif') { $fp = fopen($target, 'rb'); $targetcontent = fread($fp, 9999999); fclose($fp); $animatedgif = strpos($targetcontent, 'NETSCAPE2.0') === FALSE ? 0 : 1; } if ($watermark_logo && $wmwidth > 10 && $wmheight > 10 && !$animatedgif) { switch ($attachinfo['mime']) { case 'image/jpeg': $dst_photo = imageCreateFromJPEG($target); break; case 'image/gif': $dst_photo = imageCreateFromGIF($target); break; case 'image/png': $dst_photo = imageCreateFromPNG($target); break; } switch ($watermarkstatus) { case 1: $x = +5; $y = +5; break; case 2: $x = ($logo_w + $img_w) / 2; $y = +5; break; case 3: $x = $img_w - $logo_w - 5; $y = +5; break; case 4: $x = +5; $y = ($logo_h + $img_h) / 2; break; case 5: $x = ($logo_w + $img_w) / 2; $y = ($logo_h + $img_h) / 2; break; case 6: $x = $img_w - $logo_w; $y = ($logo_h + $img_h) / 2; break; case 7: $x = +5; $y = $img_h - $logo_h - 5; break; case 8: $x = ($logo_w + $img_w) / 2; $y = $img_h - $logo_h; break; case 9: $x = $img_w - $logo_w - 5; $y = $img_h - $logo_h - 5; break; } imageAlphaBlending($watermark_logo, FALSE); imagesavealpha($watermark_logo, TRUE); imageCopyMerge($dst_photo, $watermark_logo, $x, $y, 0, 0, $logo_w, $logo_h, $watermarktrans); switch ($attachinfo['mime']) { case 'image/jpeg': imageJPEG($dst_photo, $target); break; case 'image/gif': imageGIF($dst_photo, $target); break; case 'image/png': imagePNG($dst_photo, $target); break; } } } }
function create_watermark($file) { //文件不存在则返回 if (!file_exists($this->watermark_file) || !file_exists($file)) { return; } if (!function_exists('getImageSize')) { return; } //检查GD支持的文件类型 $gd_allow_types = array(); if (function_exists('ImageCreateFromGIF')) { $gd_allow_types['image/gif'] = 'ImageCreateFromGIF'; } if (function_exists('ImageCreateFromPNG')) { $gd_allow_types['image/png'] = 'ImageCreateFromPNG'; } if (function_exists('ImageCreateFromJPEG')) { $gd_allow_types['image/jpeg'] = 'ImageCreateFromJPEG'; } //获取文件信息 $fileinfo = getImageSize($file); $wminfo = getImageSize($this->watermark_file); if ($fileinfo[0] < $wminfo[0] || $fileinfo[1] < $wminfo[1]) { return; } if (array_key_exists($fileinfo['mime'], $gd_allow_types)) { if (array_key_exists($wminfo['mime'], $gd_allow_types)) { // 从文件创建图像 $temp = $gd_allow_types[$fileinfo['mime']]($file); $temp_wm = $gd_allow_types[$wminfo['mime']]($this->watermark_file); // 水印位置 switch ($this->watermark_pos) { case 1: //顶部居左 $dst_x = 0; $dst_y = 0; break; case 2: //顶部居中 $dst_x = ($fileinfo[0] - $wminfo[0]) / 2; $dst_y = 0; break; case 3: //顶部居右 $dst_x = $fileinfo[0]; $dst_y = 0; break; case 4: //底部居左 $dst_x = 0; $dst_y = $fileinfo[1]; break; case 5: //底部居中 $dst_x = ($fileinfo[0] - $wminfo[0]) / 2; $dst_y = $fileinfo[1]; break; case 6: //底部居右 $dst_x = $fileinfo[0] - $wminfo[0]; $dst_y = $fileinfo[1] - $wminfo[1]; break; default: //随机 $dst_x = mt_rand(0, $fileinfo[0] - $wminfo[0]); $dst_y = mt_rand(0, $fileinfo[1] - $wminfo[1]); } if (function_exists('ImageAlphaBlending')) { ImageAlphaBlending($temp_wm, True); } // 设定图像的混色模式 if (function_exists('ImageSaveAlpha')) { ImageSaveAlpha($temp_wm, True); } // 保存完整的 alpha 通道信息 //为图像添加水印 if (function_exists('imageCopyMerge')) { ImageCopyMerge($temp, $temp_wm, $dst_x, $dst_y, 0, 0, $wminfo[0], $wminfo[1], $this->watermark_trans); } else { ImageCopyMerge($temp, $temp_wm, $dst_x, $dst_y, 0, 0, $wminfo[0], $wminfo[1]); } // 保存图片 switch ($fileinfo['mime']) { case 'image/jpeg': @imageJPEG($temp, $file); break; case 'image/png': @imagePNG($temp, $file); break; case 'image/gif': @imageGIF($temp, $file); break; } // 销毁零时图像 @imageDestroy($temp); @imageDestroy($temp_wm); } } }
function convertImage($type) { /* check the converted image type availability, if it is not available, it will be casted to jpeg :) */ $validtype = $this->validateType($type); if ($this->output) { /* show the image */ switch ($validtype) { case 'jpeg': case 'jpg': header("Content-type: image/jpeg"); if ($this->imtype == 'gif' or $this->imtype == 'png') { $image = $this->replaceTransparentWhite($this->im); imageJPEG($image); } else { imageJPEG($this->im); } break; case 'gif': header("Content-type: image/gif"); imageGIF($this->im); break; case 'png': header("Content-type: image/png"); imagePNG($this->im); break; case 'wbmp': header("Content-type: image/vnd.wap.wbmp"); imageWBMP($this->im); break; case 'swf': header("Content-type: application/x-shockwave-flash"); $this->imageSWF($this->im); break; } // Memory cleanup @imagedestroy($this->im); } else { /* save the image */ switch ($validtype) { case 'jpeg': case 'jpg': if ($this->imtype == 'gif' or $this->imtype == 'png') { /* replace transparent with white */ $image = $this->replaceTransparentWhite($this->im); imageJPEG($image, $this->finalFilePath . $this->imname . ".jpg"); } else { imageJPEG($this->im, $this->finalFilePath . $this->imname . ".jpg"); } break; case 'gif': imageGIF($this->im, $this->finalFilePath . $this->imname . ".gif"); break; case 'png': imagePNG($this->im, $this->finalFilePath . $this->imname . ".png"); break; case 'wbmp': imageWBMP($this->im, $this->finalFilePath . $this->imname . ".wbmp"); break; case 'swf': $this->imageSWF($this->im, $this->finalFilePath . $this->imname . ".swf"); break; } // Memory cleanup @imagedestroy($this->im); } }
/** * this function uses the gdimage library to resize an image and save it to a path * or replace the existing file. * * @param string $cInput [file path for the source image] * @param string $cOutput [the output path] * @param integer $nH [the image height] * @param integer $nW [the image width] * @param string $xType [alternate ways to crop image] * @param integer $nQuality [image quality] * */ public function resizeImage($cInput, $cOutput, $nH = 1600, $nW = 2560, $xType = 'normal', $nQuality = 100) { if (function_exists('imagecreatefromgif')) { $src_img = ''; $nH == $nW ? $xType = 'square' : false; $cOutput == null ? $cOutput = $cInput : false; $cType = strtolower(substr(stripslashes($cInput), strrpos(stripslashes($cInput), '.'))); if ($cType == '.gif' || $cType == 'image/gif') { $src_img = imagecreatefromgif($cInput); /* Attempt to open */ $cType = 'image/gif'; } elseif ($cType == '.png' || $cType == 'image/png' || $cType == 'image/x-png') { $src_img = imagecreatefrompng($cInput); /* Attempt to open */ $cType = 'image/x-png'; } elseif ($cType == '.bmp' || $cType == 'image/bmp') { $src_img = imagecreatefrombmp($cInput); /* Attempt to open */ $cType = 'image/bmp'; } elseif ($cType == '.jpg' || $cType == '.jpeg' || $cType == 'image/jpg' || $cType == 'image/jpeg' || $cType == 'image/pjpeg') { $src_img = imagecreatefromjpeg($cInput); /* Attempt to open */ $cType = 'image/jpeg'; } else { } if (!$src_img) { $src_img = imagecreatefromgif(FOONSTER_PATH . '/images/widget.gif'); /* Attempt to open */ $cType = 'image/gif'; } else { $tmp_img; list($width, $height) = getimagesize($cInput); if ($xType == 'square' && $width != $height) { $biggestSide = ''; $cropPercent = 0.5; $cropWidth = 0; $cropHeight = 0; $c1 = array(); if ($width > $height) { $biggestSide = $width; $cropWidth = round($biggestSide * $cropPercent); $cropHeight = round($biggestSide * $cropPercent); $c1 = array("x" => ($width - $cropWidth) / 2, "y" => ($height - $cropHeight) / 2); } else { $biggestSide = $height; $cropWidth = round($biggestSide * $cropPercent); $cropHeight = round($biggestSide * $cropPercent); $c1 = array("x" => ($width - $cropWidth) / 2, "y" => ($height - $cropHeight) / 7); } $thumbSize = $nH; if ($cType == 'image/gif') { $tmp_img = imagecreate($thumbSize, $thumbSize); imagecolortransparent($tmp_img, imagecolorallocate($tmp_img, 0, 0, 0)); imagecopyresized($tmp_img, $src_img, 0, 0, $c1['x'], $c1['y'], $thumbSize, $thumbSize, $cropWidth, $cropHeight); } elseif ($cType == 'image/x-png') { $tmp_img = imagecreatetruecolor($thumbSize, $thumbSize); imagecopyresampled($tmp_img, $src_img, 0, 0, $c1['x'], $c1['y'], $thumbSize, $thumbSize, $cropWidth, $cropHeight); } elseif ($cType == 'image/bmp') { $tmp_img = imagecreatetruecolor($thumbSize, $thumbSize); imagecopyresampled($tmp_img, $src_img, 0, 0, $c1['x'], $c1['y'], $thumbSize, $thumbSize, $cropWidth, $cropHeight); } elseif ($cType == 'image/jpeg') { $tmp_img = imagecreatetruecolor($thumbSize, $thumbSize); imagecopyresampled($tmp_img, $src_img, 0, 0, $c1['x'], $c1['y'], $thumbSize, $thumbSize, $cropWidth, $cropHeight); } else { $tmp_img = imagecreatetruecolor($thumbSize, $thumbSize); imagecopyresampled($tmp_img, $src_img, 0, 0, $c1['x'], $c1['y'], $thumbSize, $thumbSize, $cropWidth, $cropHeight); } imagedestroy($src_img); $src_img = $tmp_img; } else { $ow = imagesx($src_img); $oh = imagesy($src_img); if ($nH == 0 && $nW == 0) { $nH = $oh; $nW = $ow; } if ($nH == 0) { $nH = $nW; } if ($nW == 0) { $nW = $nH; } if ($nH > $oh && $nW > $ow) { $width = $ow; $height = $oh; } else { if ($nW && $ow < $oh) { $nW = $nH / $oh * $ow; } else { $nH = $nW / $ow * $oh; } $width = $nW; $height = $nH; } if ($cType == 'image/gif') { $tmp_img = imagecreate($width, $height); imagecolortransparent($tmp_img, imagecolorallocate($tmp_img, 0, 0, 0)); imagecopyresized($tmp_img, $src_img, 0, 0, $off_w, $off_h, $width, $height, $ow, $oh); } elseif ($cType == 'image/x-png') { $tmp_img = imagecreatetruecolor($width, $height); imagecopyresampled($tmp_img, $src_img, 0, 0, $off_w, $off_h, $width, $height, $ow, $oh); } elseif ($cType == 'image/bmp') { $tmp_img = imagecreatetruecolor($width, $height); imagecopyresampled($tmp_img, $src_img, 0, 0, $off_w, $off_h, $width, $height, $ow, $oh); } elseif ($cType == 'image/jpeg') { $tmp_img = imagecreatetruecolor($width, $height); imagecopyresampled($tmp_img, $src_img, 0, 0, $off_w, $off_h, $width, $height, $ow, $oh); } else { $tmp_img = imagecreatetruecolor($width, $height); imagecopyresampled($tmp_img, $src_img, 0, 0, $off_w, $off_h, $width, $height, $ow, $oh); } imagedestroy($src_img); $src_img = $tmp_img; } } // set the output if ($cType == 'image/gif') { imageGIF($src_img, $cOutput); } elseif ($cType == 'image/x-png') { imagePNG($src_img, $cOutput); } elseif ($cType == 'image/bmp') { imageJPEG($src_img, $cOutput, $nQuality); } elseif ($cType == 'image/jpeg') { imageJPEG($src_img, $cOutput, $nQuality); } else { imageJPEG($src_img, $cOutput, $nQuality); } } }
<?php header("Content-type: image/gif"); dl('php3_gd.dll'); //header("Content-type: image/png"); $image = imageCreate(100, 100); imageGIF($image); //imagePNG($image); // create an interlaced image for better loading in the browser imageInterlace($image, 1); // mark background color as being transparent $colorBackgr = imageColorAllocate($image, 192, 192, 192); imageColorTransparent($image, $colorBackgr);
function generateImage($save = '', $show = true) { if ($this->img["format"] == "GIF" && !$this->gifsupport) { // --- kein caching -> gif ausgeben Header("Content-Type: image/" . $this->img["format"]); readfile($this->imgfile); exit; } $this->resampleImage(); if ($this->img["format"] == "JPG" || $this->img["format"] == "JPEG") { imageJPEG($this->img["des"], $save, $this->img["quality"]); } elseif ($this->img["format"] == "PNG") { imagePNG($this->img["des"], $save); } elseif ($this->img["format"] == "GIF") { imageGIF($this->img["des"], $save); } elseif ($this->img["format"] == "WBMP") { imageWBMP($this->img["des"], $save); } if ($show) { Header("Content-Type: image/" . $this->img["format"]); readfile($save); } }