/** * * long description * @global object * @param object $dst_img * @param object $src_img * @param int $dst_x * @param int $dst_y * @param int $src_x * @param int $src_y * @param int $dst_w * @param int $dst_h * @param int $src_w * @param int $src_h * @return bool * @todo Finish documenting this function */ function ImageCopyBicubic($dst_img, $src_img, $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h) { global $CFG; if (function_exists('ImageCopyResampled') and $CFG->gdversion >= 2) { return ImageCopyResampled($dst_img, $src_img, $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h); } $totalcolors = imagecolorstotal($src_img); for ($i = 0; $i < $totalcolors; $i++) { if ($colors = ImageColorsForIndex($src_img, $i)) { ImageColorAllocate($dst_img, $colors['red'], $colors['green'], $colors['blue']); } } $scaleX = ($src_w - 1) / $dst_w; $scaleY = ($src_h - 1) / $dst_h; $scaleX2 = $scaleX / 2.0; $scaleY2 = $scaleY / 2.0; for ($j = 0; $j < $dst_h; $j++) { $sY = $j * $scaleY; for ($i = 0; $i < $dst_w; $i++) { $sX = $i * $scaleX; $c1 = ImageColorsForIndex($src_img, ImageColorAt($src_img, (int) $sX, (int) $sY + $scaleY2)); $c2 = ImageColorsForIndex($src_img, ImageColorAt($src_img, (int) $sX, (int) $sY)); $c3 = ImageColorsForIndex($src_img, ImageColorAt($src_img, (int) $sX + $scaleX2, (int) $sY + $scaleY2)); $c4 = ImageColorsForIndex($src_img, ImageColorAt($src_img, (int) $sX + $scaleX2, (int) $sY)); $red = (int) (($c1['red'] + $c2['red'] + $c3['red'] + $c4['red']) / 4); $green = (int) (($c1['green'] + $c2['green'] + $c3['green'] + $c4['green']) / 4); $blue = (int) (($c1['blue'] + $c2['blue'] + $c3['blue'] + $c4['blue']) / 4); $color = ImageColorClosest($dst_img, $red, $green, $blue); ImageSetPixel($dst_img, $i + $dst_x, $j + $dst_y, $color); } } }
/** * Image to Text * * Takes a given system image and recreates it with a given string * Function accepts arbitrary elements to use for markup * * @access public * @param string * @param string * @param string * @return string */ function image_to_text($data, $txt, $new_width = NULL, $new_height = NULL, $inline_element = 'span') { $img = imagecreatefromstring($data); $width = imagesx($img); $height = imagesy($img); // add abiilty to resize on the fly if ($new_width and $new_height) { $new = imagecreatetruecolor($new_width, $new_height); imagecopyresampled($new, $img, 0, 0, 0, 0, $new_width, $new_height, $width, $height); unset($img); $img = $new; $width = $new_width; $height = $new_height; unset($new); } $counter = 0; $output = ""; for ($i = 0; $i < $height; $i++) { for ($j = 0; $j < $width; $j++) { $counter = $counter % strlen($txt); $cindex = ImageColorAt($img, $j, $i); $rgb = ImageColorsForIndex($img, $cindex); $hex = rgb2hex(array($rgb['red'], $rgb['green'], $rgb['blue'])); $output .= '<' . $inline_element . ' style="color:#' . $hex . ';">' . substr($txt, $counter, 1) . '</' . $inline_element . '>'; $counter++; } $output .= "<br />"; } return $output; }
/** * Retourne la couleur d'un pixel dans une image * * @param ressource $img * @param int $x * @param int $y * @return array|bool */ public static function GetPixelColor(&$img, $x, $y) { if (!is_resource($img)) { return false; } return @ImageColorsForIndex($img, @ImageColorAt($img, $x, $y)); }
/** * Antialias'es the pixel around x,y with weights a,b * * @param int $x X point * @param int $y Y point * @param int $a The weight of the current color * @param int $b The weight of the applied/wanted color * @param mixed $color The pixel color * @access private */ function _antialisedSubPixel($x, $y, $a, $b, $color) { $x = $this->_getX($x); $y = $this->_getX($y); if ($x >= 0 && $y >= 0 && $x < $this->getWidth() && $y < $this->getHeight()) { $tempColor = ImageColorsForIndex($this->_canvas, ImageColorAt($this->_canvas, $x, $y)); $newColor[0] = min(255, round($tempColor['red'] * $a + $color['red'] * $b)); $newColor[1] = min(255, round($tempColor['green'] * $a + $color['green'] * $b)); $newColor[2] = min(255, round($tempColor['blue'] * $a + $color['blue'] * $b)); //$newColor[3] = 0; $color = '#'; foreach ($newColor as $acolor) { $color .= sprintf('%02s', dechex($acolor)); } $newColor = $this->_color($color); //,'rgb(' . $newColor[0] . ',' . $newColor[1] . ',' . $newColor[2] .')'; ImageSetPixel($this->_canvas, $x, $y, $newColor); } }
/** * From: https://stackoverflow.com/questions/4590441/php-thumbnail-image-resizing-with-proportions */ function resize(&$img, $size) { if (!is_numeric($size)) { $size = explode('x', $size); } if (is_array($size)) { $maxwidth = $size[0]; $maxheight = $size[1]; } else { if ($size) { $maxwidth = $size; $maxheight = $size; } } $width = imagesx($img); $height = imagesy($img); if (!ALLOW_BLOATING) { if ($maxwidth > $width) { $maxwidth = $width; } if ($maxheight > $height) { $maxheight = $height; } } if ($height > $width) { $ratio = $maxheight / $height; $newheight = $maxheight; $newwidth = $width * $ratio; } else { $ratio = $maxwidth / $width; $newwidth = $maxwidth; $newheight = $height * $ratio; } $newimg = imagecreatetruecolor($newwidth, $newheight); $palsize = ImageColorsTotal($img); for ($i = 0; $i < $palsize; $i++) { $colors = ImageColorsForIndex($img, $i); ImageColorAllocate($newimg, $colors['red'], $colors['green'], $colors['blue']); } imagefill($newimg, 0, 0, IMG_COLOR_TRANSPARENT); imagesavealpha($newimg, true); imagealphablending($newimg, true); imagecopyresampled($newimg, $img, 0, 0, 0, 0, $newwidth, $newheight, $width, $height); $img = $newimg; }
public static function imageCopyResampledBicubic(&$dst_image, &$src_image, $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h) { // We should first cut the piece we are interested in from the source $src_img = ImageCreateTrueColor($src_w, $src_h); imagecopy($src_img, $src_image, 0, 0, $src_x, $src_y, $src_w, $src_h); // This one is used as temporary image $dst_img = ImageCreateTrueColor($dst_w, $dst_h); ImagePaletteCopy($dst_img, $src_img); $rX = $src_w / $dst_w; $rY = $src_h / $dst_h; $w = 0; for ($y = 0; $y < $dst_h; $y++) { $ow = $w; $w = round(($y + 1) * $rY); $t = 0; for ($x = 0; $x < $dst_w; $x++) { $r = $g = $b = 0; $a = 0; $ot = $t; $t = round(($x + 1) * $rX); for ($u = 0; $u < $w - $ow; $u++) { for ($p = 0; $p < $t - $ot; $p++) { $c = ImageColorsForIndex($src_img, ImageColorAt($src_img, $ot + $p, $ow + $u)); $r += $c['red']; $g += $c['green']; $b += $c['blue']; $a++; } } ImageSetPixel($dst_img, $x, $y, ImageColorClosest($dst_img, $r / $a, $g / $a, $b / $a)); } } // Apply the temp image over the returned image and use the destination x,y coordinates imagecopy($dst_image, $dst_img, $dst_x, $dst_y, 0, 0, $dst_w, $dst_h); // We should return true since ImageCopyResampled/ImageCopyResized do it return true; }
/** * Apply input levels to input image pointer (increasing contrast) * * @param resource $im GDlib Image Pointer * @param int $low The "low" value (close to 0) * @param int $high The "high" value (close to 255) * @return void */ public function inputLevels(&$im, $low, $high) { if ($low < $high) { $low = MathUtility::forceIntegerInRange($low, 0, 255); $high = MathUtility::forceIntegerInRange($high, 0, 255); $delta = $high - $low; $totalCols = ImageColorsTotal($im); for ($c = 0; $c < $totalCols; $c++) { $cols = ImageColorsForIndex($im, $c); $cols['red'] = MathUtility::forceIntegerInRange(($cols['red'] - $low) / $delta * 255, 0, 255); $cols['green'] = MathUtility::forceIntegerInRange(($cols['green'] - $low) / $delta * 255, 0, 255); $cols['blue'] = MathUtility::forceIntegerInRange(($cols['blue'] - $low) / $delta * 255, 0, 255); ImageColorSet($im, $c, $cols['red'], $cols['green'], $cols['blue']); } } }
/** * Given a user id this function scales and crops the user images to remove * the one pixel black border. * * @global object * @param int $id * @param string $dir * @return boolean */ function upgrade_profile_image($id, $dir = 'users') { global $CFG, $OUTPUT; $im = ImageCreateFromJPEG($CFG->dataroot . '/' . $dir . '/' . $id . '/f1.jpg'); if (function_exists('ImageCreateTrueColor') and $CFG->gdversion >= 2) { $im1 = ImageCreateTrueColor(100, 100); $im2 = ImageCreateTrueColor(35, 35); } else { $im1 = ImageCreate(100, 100); $im2 = ImageCreate(35, 35); } if (function_exists('ImageCopyResampled') and $CFG->gdversion >= 2) { ImageCopyBicubic($im1, $im, 0, 0, 2, 2, 100, 100, 96, 96); } else { imagecopy($im1, $im, 0, 0, 0, 0, 100, 100); $c = ImageColorsForIndex($im1, ImageColorAt($im1, 2, 2)); $color = ImageColorClosest($im1, $c['red'], $c['green'], $c['blue']); ImageSetPixel($im1, 0, 0, $color); $c = ImageColorsForIndex($im1, ImageColorAt($im1, 2, 97)); $color = ImageColorClosest($im1, $c['red'], $c['green'], $c['blue']); ImageSetPixel($im1, 0, 99, $color); $c = ImageColorsForIndex($im1, ImageColorAt($im1, 97, 2)); $color = ImageColorClosest($im1, $c['red'], $c['green'], $c['blue']); ImageSetPixel($im1, 99, 0, $color); $c = ImageColorsForIndex($im1, ImageColorAt($im1, 97, 97)); $color = ImageColorClosest($im1, $c['red'], $c['green'], $c['blue']); ImageSetPixel($im1, 99, 99, $color); for ($x = 1; $x < 99; $x++) { $c1 = ImageColorsForIndex($im1, ImageColorAt($im, $x, 1)); $color = ImageColorClosest($im, $c1['red'], $c1['green'], $c1['blue']); ImageSetPixel($im1, $x, 0, $color); $c2 = ImageColorsForIndex($im1, ImageColorAt($im1, $x, 98)); $color = ImageColorClosest($im, $c2['red'], $c2['green'], $c2['blue']); ImageSetPixel($im1, $x, 99, $color); } for ($y = 1; $y < 99; $y++) { $c3 = ImageColorsForIndex($im1, ImageColorAt($im, 1, $y)); $color = ImageColorClosest($im, $c3['red'], $c3['green'], $c3['blue']); ImageSetPixel($im1, 0, $y, $color); $c4 = ImageColorsForIndex($im1, ImageColorAt($im1, 98, $y)); $color = ImageColorClosest($im, $c4['red'], $c4['green'], $c4['blue']); ImageSetPixel($im1, 99, $y, $color); } } ImageCopyBicubic($im2, $im, 0, 0, 2, 2, 35, 35, 96, 96); if (function_exists('ImageJpeg')) { if (ImageJpeg($im1, $CFG->dataroot . '/' . $dir . '/' . $id . '/f1.jpg', 90) and ImageJpeg($im2, $CFG->dataroot . '/' . $dir . '/' . $id . '/f2.jpg', 95)) { @chmod($CFG->dataroot . '/' . $dir . '/' . $id . '/f1.jpg', 0666); @chmod($CFG->dataroot . '/' . $dir . '/' . $id . '/f2.jpg', 0666); return 1; } } else { echo $OUTPUT->notification('PHP has not been configured to support JPEG images. Please correct this.'); } return 0; }
public function GD2BMPstring(&$gd_image) { // Author: James Heinrich // Purpose: Save file as type bmp // Param in: The image canvas (passed as ref) $imageX = ImageSX($gd_image); $imageY = ImageSY($gd_image); $BMP = ''; for ($y = $imageY - 1; $y >= 0; $y--) { $thisline = ''; for ($x = 0; $x < $imageX; $x++) { $argb = ImageColorsForIndex($gd_image, @ImageColorAt($gd_image, $x, $y)); $thisline .= chr($argb['blue']) . chr($argb['green']) . chr($argb['red']); } while (strlen($thisline) % 4) { $thisline .= ""; } $BMP .= $thisline; } $bmpSize = strlen($BMP) + 14 + 40; // BITMAPFILEHEADER [14 bytes] - http://msdn.microsoft.com/library/en-us/gdi/bitmaps_62uq.asp $BITMAPFILEHEADER = 'BM'; // WORD bfType; $BITMAPFILEHEADER .= $this->LittleEndian2String($bmpSize, 4); // DWORD bfSize; $BITMAPFILEHEADER .= $this->LittleEndian2String(0, 2); // WORD bfReserved1; $BITMAPFILEHEADER .= $this->LittleEndian2String(0, 2); // WORD bfReserved2; $BITMAPFILEHEADER .= $this->LittleEndian2String(54, 4); // DWORD bfOffBits; // BITMAPINFOHEADER - [40 bytes] http://msdn.microsoft.com/library/en-us/gdi/bitmaps_1rw2.asp $BITMAPINFOHEADER = $this->LittleEndian2String(40, 4); // DWORD biSize; $BITMAPINFOHEADER .= $this->LittleEndian2String($imageX, 4); // LONG biWidth; $BITMAPINFOHEADER .= $this->LittleEndian2String($imageY, 4); // LONG biHeight; $BITMAPINFOHEADER .= $this->LittleEndian2String(1, 2); // WORD biPlanes; $BITMAPINFOHEADER .= $this->LittleEndian2String(24, 2); // WORD biBitCount; $BITMAPINFOHEADER .= $this->LittleEndian2String(0, 4); // DWORD biCompression; $BITMAPINFOHEADER .= $this->LittleEndian2String(0, 4); // DWORD biSizeImage; $BITMAPINFOHEADER .= $this->LittleEndian2String(2835, 4); // LONG biXPelsPerMeter; $BITMAPINFOHEADER .= $this->LittleEndian2String(2835, 4); // LONG biYPelsPerMeter; $BITMAPINFOHEADER .= $this->LittleEndian2String(0, 4); // DWORD biClrUsed; $BITMAPINFOHEADER .= $this->LittleEndian2String(0, 4); // DWORD biClrImportant; return $BITMAPFILEHEADER . $BITMAPINFOHEADER . $BMP; }
function Resize_Image($src, $width = 0, $height = 0, $dst) { if ($height <= 0 && $width <= 0) { return false; } // Setting defaults and meta $image = ''; $final_width = 0; $final_height = 0; list($width_old, $height_old, $image_type) = GetImageSize($src); // Calculating proportionality if ($width == 0) { $factor = $height / $height_old; } elseif ($height == 0) { $factor = $width / $width_old; } else { $factor = Min($width / $width_old, $height / $height_old); } $final_width = Round($width_old * $factor); $final_height = Round($height_old * $factor); // Loading image to memory according to type switch ($image_type) { case IMAGETYPE_GIF: $image = imagecreatefromgif($src); break; case IMAGETYPE_JPEG: $image = imagecreatefromjpeg($src); break; case IMAGETYPE_PNG: $image = imagecreatefrompng($src); break; default: return false; } // This is the resizing/resampling/transparency-preserving magic $image_resized = ImageCreateTrueColor($final_width, $final_height); if ($image_type == IMAGETYPE_GIF || $image_type == IMAGETYPE_PNG) { $transparency = ImageColorTransparent($image); if ($image_type == IMAGETYPE_GIF && $transparency >= 0) { list($r, $g, $b) = Array_Values(ImageColorsForIndex($image, $transparency)); $transparency = ImageColorAllocate($image_resized, $r, $g, $b); Imagefill($image_resized, 0, 0, $transparency); ImageColorTransparent($image_resized, $transparency); } elseif ($image_type == IMAGETYPE_PNG) { ImageAlphaBlending($image_resized, false); $color = ImageColorAllocateAlpha($image_resized, 0, 0, 0, 127); ImageFill($image_resized, 0, 0, $color); ImageSaveAlpha($image_resized, true); } } ImageCopyResampled($image_resized, $image, 0, 0, 0, 0, $final_width, $final_height, $width_old, $height_old); // Writing image switch ($image_type) { case IMAGETYPE_GIF: imagegif($image_resized, $dst); break; case IMAGETYPE_JPEG: imagejpeg($image_resized, $dst, 85); break; case IMAGETYPE_PNG: imagepng($image_resized, $dst); break; default: return false; } }
/** * From: https://stackoverflow.com/questions/4590441/php-thumbnail-image-resizing-with-proportions */ function resize(&$img, $size) { $pm = new PictshareModel(); $sd = $pm->sizeStringToWidthHeight($size); $maxwidth = $sd['width']; $maxheight = $sd['height']; $width = imagesx($img); $height = imagesy($img); if (!ALLOW_BLOATING) { if ($maxwidth > $width) { $maxwidth = $width; } if ($maxheight > $height) { $maxheight = $height; } } if ($height > $width) { $ratio = $maxheight / $height; $newheight = $maxheight; $newwidth = $width * $ratio; } else { $ratio = $maxwidth / $width; $newwidth = $maxwidth; $newheight = $height * $ratio; } $newimg = imagecreatetruecolor($newwidth, $newheight); $palsize = ImageColorsTotal($img); for ($i = 0; $i < $palsize; $i++) { $colors = ImageColorsForIndex($img, $i); ImageColorAllocate($newimg, $colors['red'], $colors['green'], $colors['blue']); } imagefill($newimg, 0, 0, IMG_COLOR_TRANSPARENT); imagesavealpha($newimg, true); imagealphablending($newimg, true); imagecopyresampled($newimg, $img, 0, 0, 0, 0, $newwidth, $newheight, $width, $height); $img = $newimg; }
<?php $transparent = ImageColorsForIndex($image, ImageColorTransparent($image)); print_r($transparent);
// Otherwise, Assume Width Is Greater Than Height (Will Produce Same Result If Width Is Equal To Height) $zoom = $twidth / $currwidth; // Length Ratio For Height $newwidth = $twidth; // Width Is Equal To Max Width $newheight = $currheight * $zoom; // Creates The New Height } $dimg = imagecreate($newwidth, $newheight); // Make New Image For Thumbnail imagetruecolortopalette($simg, false, 256); // Create New Color Pallete $palsize = ImageColorsTotal($simg); for ($i = 0; $i < $palsize; $i++) { // Counting Colors In The Image $colors = ImageColorsForIndex($simg, $i); // Number Of Colors Used ImageColorAllocate($dimg, $colors['red'], $colors['green'], $colors['blue']); // Tell The Server What Colors This Image Will Use } imagecopyresized($dimg, $simg, 0, 0, 0, 0, $newwidth, $newheight, $currwidth, $currheight); // Copy Resized Image To The New Image (So We Can Save It) imagejpeg($dimg, "{$tdir}" . $_SESSION['user']['username'] . '.jpg'); // Saving The Image imagedestroy($simg); // Destroying The Temporary Image imagedestroy($dimg); // Destroying The Other Temporary Image print 'Image thumbnail created successfully.'; // Resize successful $user = new User(array('id' => $_SESSION['user']['id'], 'avatar' => $_SESSION['user']['username'] . '.jpg'));
private function GetPixelColor(&$img, $x, $y) { # Author: James Heinrich # Purpose: # Param in: # Param out: # Reference: # Notes:# if (!is_resource($img)) { return false; } return @ImageColorsForIndex($img, @ImageColorAt($img, $x, $y)); }
public function __invoke() { $view = $this->view; $pixObject = $this->pixObject; $id = $pixObject->getId(); $title = $pixObject->getTitle(); $pixname = $pixObject->getPicture(); $filepix = "/usr/local/apache2/html/uploads/"; $filepix .= $this->username; $filepix .= "/pix/"; $pix = $filepix . $pixname; // Check to see that the thumbnail exists. $thumbPixName = "large_" . $pixname; $thumbPix = $filepix . $thumbPixName; if (!file_exists($thumbPix)) { $img = imagecreatefromjpeg($pix); $width = imagesx($img); $height = imagesy($img); $whlog = "width: " . $width . " height " . $height; //determine which side is the longest to use in calculating length of the shorter side, since the longest will be the max size for whichever side is longest. if ($height > $width) { $ratio = 160 / $height; $newheight = 160; $newwidth = $width * $ratio; } else { $ratio = 160 / $width; $newwidth = 160; $newheight = $height * $ratio; } $whlog = "newwidth: " . $newwidth . " newheight " . $newheight; //create new image resource to hold the resized image $newimg = imagecreatetruecolor($newwidth, $newheight); $palsize = ImageColorsTotal($img); //Get palette size for original image for ($i = 0; $i < $palsize; $i++) { $colors = ImageColorsForIndex($img, $i); ImageColorAllocate($newimg, $colors['red'], $colors['green'], $colors['blue']); } //copy original image into new image at new size. imagecopyresized($newimg, $img, 0, 0, 0, 0, $newwidth, $newheight, $width, $height); imagejpeg($newimg, $thumbPix); //$output file is the path/filename where you wish to save the file. } $base = 'https://newhollandpress.com/pix/view/'; $base .= urlencode($id); return $view->partial("/items/pix.phtml"); /* $output = "<div id='" . $this->itemId . "' class='itemHelper_Wide'>"; $output .= "<ul>"; $output .= "<li>"; $output .= "<span>Pix</span>"; $output .= "</li>"; $output .= "<li>"; $output .= "<a href='" . $base . "'>" . $id . "</a>"; $output .= "</li>"; $output .= "<li>"; $output .= $pixObject->getPicture(); $output .= "</li>"; $output .= "<li>"; $output .= "<img src='https://www.newhollandpress.com/uploads/"; $output .= $this->username; $output .= "/pix/"; $output .= $thumbPixName; $output .= "'>"; $output .= "</li>"; $output .= "</ul>"; $output .= "</div>"; return $output; * */ }
function save_resize($tempDir, $imgurl, $w, $h, $destDir) { //print ("save_resize...\n<br />"); //print ("$tempDir\n<br />"); //print ("$imgurl\n<br />"); //print ("$w<br />\n"); //print ("$h<br />\n"); //print ("$destDir<br />\n"); $simg = imagecreatefromjpeg("{$tempDir}" . $imgurl); // Make A New Temporary Image To Create The Thumbanil From $currwidth = imagesx($simg); // Current Image Width $currheight = imagesy($simg); // Current Image Height //print ("$currwidth<br />\n"); //print ("$currheight<br />\n"); //if ($currheight > $currwidth) { // If Height Is Greater Than Width // $zoom = $w / $currheight; // Length Ratio For Width // $newheight = $h; // Height Is Equal To Max Height // $newwidth = $currwidth * $zoom; // Creates The New Width //} else { // Otherwise, Assume Width Is Greater Than Height (Will Produce Same Result If Width Is Equal To Height) $zoom = $w / $currwidth; // Length Ratio For Height $newwidth = $w; // Width Is Equal To Max Width $newheight = $currheight * $zoom; // Creates The New Height //} //print ("$newwidth<br />\n"); //print ("$newheight<br />\n"); $dimg = imagecreate($newwidth, $newheight); // Make New Image For Thumbnail imagetruecolortopalette($simg, false, 256); // Create New Color Pallete $palsize = ImageColorsTotal($simg); for ($i = 0; $i < $palsize; $i++) { // Counting Colors In The Image $colors = ImageColorsForIndex($simg, $i); // Number Of Colors Used ImageColorAllocate($dimg, $colors['red'], $colors['green'], $colors['blue']); // Tell The Server What Colors This Image Will Use } imagecopyresized($dimg, $simg, 0, 0, 0, 0, $newwidth, $newheight, $currwidth, $currheight); // Copy Resized Image To The New Image (So We Can Save It) imagejpeg($dimg, "{$destDir}" . $imgurl); // Saving The Image //print ("$destDir" . $imgurl . "<br />"); imagedestroy($simg); // Destroying The Temporary Image imagedestroy($dimg); // Destroying The Other Temporary Image return $newheight; //print ("save_resize DONE..."); }
/** * Resize the image proportionally to the given width/height * * Note: Some code used in this method is adapted from code found in comments at php.net for the GD functions * * @param int $targetWidth Target width in pixels, or 0 for proportional to height * @param int $targetHeight Target height in pixels, or 0 for proportional to width. Optional-if not specified, 0 is assumed. * @return bool True if the resize was successful * * @todo this method has become too long and needs to be split into smaller dedicated parts * */ public function ___resize($targetWidth, $targetHeight = 0) { $orientations = null; // @horst $needRotation = $this->autoRotation !== true ? false : ($this->checkOrientation($orientations) && (!empty($orientations[0]) || !empty($orientations[1])) ? true : false); $needResizing = true; // $this->isResizeNecessary($targetWidth, $targetHeight); //if(!$needResizing && !$needRotation) return true; $source = $this->filename; $dest = str_replace("." . $this->extension, "_tmp." . $this->extension, $source); $image = null; switch ($this->imageType) { // @teppo case IMAGETYPE_GIF: $image = @imagecreatefromgif($source); break; case IMAGETYPE_PNG: $image = @imagecreatefrompng($source); break; case IMAGETYPE_JPEG: $image = @imagecreatefromjpeg($source); break; } if (!$image) { return false; } if ($this->imageType != IMAGETYPE_PNG) { // @horst: linearize gamma to 1.0 - we do not use gamma correction with png because it doesn't respect transparency imagegammacorrect($image, 2.0, 1.0); } if ($needRotation) { // @horst $image = $this->imRotate($image, $orientations[0]); if ($orientations[0] == 90 || $orientations[0] == 270) { // we have to swap width & height now! $tmp = array($targetWidth, $targetHeight); $targetWidth = $tmp[1]; $targetHeight = $tmp[0]; $tmp = array($this->getWidth(), $this->getHeight()); $this->setImageInfo($tmp[1], $tmp[0]); } if ($orientations[1] > 0) { $image = $this->imFlip($image, $orientations[1] == 2 ? true : false); } } if ($needResizing) { list($gdWidth, $gdHeight, $targetWidth, $targetHeight) = $this->getResizeDimensions($targetWidth, $targetHeight); $thumb = imagecreatetruecolor($gdWidth, $gdHeight); if ($this->imageType == IMAGETYPE_PNG) { // @adamkiss PNG transparency imagealphablending($thumb, false); imagesavealpha($thumb, true); } else { if ($this->imageType == IMAGETYPE_GIF) { // @mrx GIF transparency $transparentIndex = ImageColorTransparent($image); $transparentColor = $transparentIndex != -1 ? ImageColorsForIndex($image, $transparentIndex) : 0; if (!empty($transparentColor)) { $transparentNew = ImageColorAllocate($thumb, $transparentColor['red'], $transparentColor['green'], $transparentColor['blue']); $transparentNewIndex = ImageColorTransparent($thumb, $transparentNew); ImageFill($thumb, 0, 0, $transparentNewIndex); } } else { $bgcolor = imagecolorallocate($thumb, 0, 0, 0); imagefilledrectangle($thumb, 0, 0, $gdWidth, $gdHeight, $bgcolor); imagealphablending($thumb, false); } } imagecopyresampled($thumb, $image, 0, 0, 0, 0, $gdWidth, $gdHeight, $this->image['width'], $this->image['height']); $thumb2 = imagecreatetruecolor($targetWidth, $targetHeight); if ($this->imageType == IMAGETYPE_PNG) { // @adamkiss PNG transparency imagealphablending($thumb2, false); imagesavealpha($thumb2, true); } else { if ($this->imageType == IMAGETYPE_GIF) { // @mrx GIF transparency if (!empty($transparentColor)) { $transparentNew = ImageColorAllocate($thumb2, $transparentColor['red'], $transparentColor['green'], $transparentColor['blue']); $transparentNewIndex = ImageColorTransparent($thumb2, $transparentNew); ImageFill($thumb2, 0, 0, $transparentNewIndex); } } else { $bgcolor = imagecolorallocate($thumb2, 0, 0, 0); imagefilledrectangle($thumb2, 0, 0, $targetWidth, $targetHeight, 0); imagealphablending($thumb2, false); } } $w1 = $gdWidth / 2 - $targetWidth / 2; $h1 = $gdHeight / 2 - $targetHeight / 2; if (is_string($this->cropping)) { switch ($this->cropping) { // @interrobang crop directions case 'nw': $w1 = 0; $h1 = 0; break; case 'n': $h1 = 0; break; case 'ne': $w1 = $gdWidth - $targetWidth; $h1 = 0; break; case 'w': $w1 = 0; break; case 'e': $w1 = $gdWidth - $targetWidth; break; case 'sw': $w1 = 0; $h1 = $gdHeight - $targetHeight; break; case 's': $h1 = $gdHeight - $targetHeight; break; case 'se': $w1 = $gdWidth - $targetWidth; $h1 = $gdHeight - $targetHeight; break; default: // center or false, we do nothing } } else { if (is_array($this->cropping)) { // @interrobang + @u-nikos if (strpos($this->cropping[0], '%') === false) { $pointX = (int) $this->cropping[0]; } else { $pointX = $gdWidth * ((int) $this->cropping[0] / 100); } if (strpos($this->cropping[1], '%') === false) { $pointY = (int) $this->cropping[1]; } else { $pointY = $gdHeight * ((int) $this->cropping[1] / 100); } if ($pointX < $targetWidth / 2) { $w1 = 0; } else { if ($pointX > $gdWidth - $targetWidth / 2) { $w1 = $gdWidth - $targetWidth; } else { $w1 = $pointX - $targetWidth / 2; } } if ($pointY < $targetHeight / 2) { $h1 = 0; } else { if ($pointY > $gdHeight - $targetHeight / 2) { $h1 = $gdHeight - $targetHeight; } else { $h1 = $pointY - $targetHeight / 2; } } } } imagecopyresampled($thumb2, $thumb, 0, 0, $w1, $h1, $targetWidth, $targetHeight, $targetWidth, $targetHeight); if ($this->sharpening && $this->sharpening != 'none') { $image = $this->imSharpen($thumb2, $this->sharpening); } // @horst } // write to file $result = false; switch ($this->imageType) { case IMAGETYPE_GIF: // correct gamma from linearized 1.0 back to 2.0 imagegammacorrect($thumb2, 1.0, 2.0); $result = imagegif($thumb2, $dest); break; case IMAGETYPE_PNG: // convert 1-100 (worst-best) scale to 0-9 (best-worst) scale for PNG $quality = round(abs(($this->quality - 100) / 11.111111)); $result = imagepng($thumb2, $dest, $quality); break; case IMAGETYPE_JPEG: // correct gamma from linearized 1.0 back to 2.0 imagegammacorrect($thumb2, 1.0, 2.0); $result = imagejpeg($thumb2, $dest, $this->quality); break; } @imagedestroy($image); // @horst if (isset($thumb) && is_resource($thumb)) { @imagedestroy($thumb); } // @horst if (isset($thumb2) && is_resource($thumb2)) { @imagedestroy($thumb2); } // @horst if ($result === false) { if (is_file($dest)) { @unlink($dest); } return false; } unlink($source); rename($dest, $source); // @horst: if we've retrieved IPTC-Metadata from sourcefile, we write it back now if ($this->iptcRaw) { $content = iptcembed($this->iptcPrepareData(), $this->filename); if ($content !== false) { $dest = preg_replace('/\\.' . $this->extension . '$/', '_tmp.' . $this->extension, $this->filename); if (strlen($content) == @file_put_contents($dest, $content, LOCK_EX)) { // on success we replace the file unlink($this->filename); rename($dest, $this->filename); } else { // it was created a temp diskfile but not with all data in it if (file_exists($dest)) { @unlink($dest); } } } } $this->loadImageInfo($this->filename); $this->modified = true; return true; }
/** * Resample the image * http://www.php.net/manual/en/function.imagecopyresized.php */ protected function resampleBicubic(&$dst, &$src, $dstx, $dsty, $srcx, $srcy, $w, $h, $zoomX, $zoomY = '') { if (!$zoomY) { $zoomY = $zoomX; } $palsize = ImageColorsTotal($src); for ($i = 0; $i < $palsize; $i++) { $colors = ImageColorsForIndex($src, $i); ImageColorAllocate($dst, $colors['red'], $colors['green'], $colors['blue']); } $zoomX2 = (int) ($zoomX / 2); $zoomY2 = (int) ($zoomY / 2); $dstX = imagesx($dst); $dstY = imagesy($dst); $srcX = imagesx($src); $srcY = imagesy($src); for ($j = 0; $j < $h - $dsty; $j++) { $sY = (int) ($j * $zoomY) + $srcy; $y13 = $sY + $zoomY2; $dY = $j + $dsty; if ($sY >= $srcY or $dY >= $dstY or $y13 >= $srcY) { break 1; } for ($i = 0; $i < $w - $dstx; $i++) { $sX = (int) ($i * $zoomX) + $srcx; $x34 = $sX + $zoomX2; $dX = $i + $dstx; if ($sX >= $srcX or $dX >= $dstX or $x34 >= $srcX) { break 1; } $c1 = ImageColorsForIndex($src, ImageColorAt($src, $sX, $y13)); $c2 = ImageColorsForIndex($src, ImageColorAt($src, $sX, $sY)); $c3 = ImageColorsForIndex($src, ImageColorAt($src, $x34, $y13)); $c4 = ImageColorsForIndex($src, ImageColorAt($src, $x34, $sY)); $r = ($c1['red'] + $c2['red'] + $c3['red'] + $c4['red']) / 4; $g = ($c1['green'] + $c2['green'] + $c3['green'] + $c4['green']) / 4; $b = ($c1['blue'] + $c2['blue'] + $c3['blue'] + $c4['blue']) / 4; ImageSetPixel($dst, $dX, $dY, ImageColorClosest($dst, $r, $g, $b)); } } }
function ImageCopyResampleBicubic($dst_img, $src_img, $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h) { for ($i = 0; $i < imagecolorstotal($src_img); $i++) { $colors = ImageColorsForIndex($src_img, $i); ImageColorAllocate($dst_img, $colors['red'], $colors['green'], $colors['blue']); } $scaleX = ($src_w - 1) / $dst_w; $scaleY = ($src_h - 1) / $dst_h; $scaleX2 = $scaleX / 2.0; $scaleY2 = $scaleY / 2.0; for ($j = $src_y; $j < $src_y + $dst_h; $j++) { $sY = $j * $scaleY; for ($i = $src_x; $i < $src_x + $dst_w; $i++) { $sX = $i * $scaleX; $c1 = ImageColorsForIndex($src_img, ImageColorAt($src_img, (int) $sX, (int) $sY + $scaleY2)); $c2 = ImageColorsForIndex($src_img, ImageColorAt($src_img, (int) $sX, (int) $sY)); $c3 = ImageColorsForIndex($src_img, ImageColorAt($src_img, (int) $sX + $scaleX2, (int) $sY + $scaleY2)); $c4 = ImageColorsForIndex($src_img, ImageColorAt($src_img, (int) $sX + $scaleX2, (int) $sY)); $red = (int) (($c1['red'] + $c2['red'] + $c3['red'] + $c4['red']) / 4); $green = (int) (($c1['green'] + $c2['green'] + $c3['green'] + $c4['green']) / 4); $blue = (int) (($c1['blue'] + $c2['blue'] + $c3['blue'] + $c4['blue']) / 4); $color = ImageColorClosest($dst_img, $red, $green, $blue); ImageSetPixel($dst_img, $dst_x + $i - $src_x, $dst_y + $j - $src_y, $color); } } }
/** * Apply input levels to input image pointer (increasing contrast) * * @param integer $im GDlib Image Pointer * @param integer $low The "low" value (close to 0) * @param integer $high The "high" value (close to 255) * @param boolean $swap If swap, then low and high are swapped. (Useful for negated masks...) * @return void * @todo Define visibility */ public function inputLevels(&$im, $low, $high, $swap = '') { if ($low < $high) { $low = \TYPO3\CMS\Core\Utility\MathUtility::forceIntegerInRange($low, 0, 255); $high = \TYPO3\CMS\Core\Utility\MathUtility::forceIntegerInRange($high, 0, 255); if ($swap) { $temp = $low; $low = 255 - $high; $high = 255 - $temp; } $delta = $high - $low; $totalCols = ImageColorsTotal($im); for ($c = 0; $c < $totalCols; $c++) { $cols = ImageColorsForIndex($im, $c); $cols['red'] = \TYPO3\CMS\Core\Utility\MathUtility::forceIntegerInRange(($cols['red'] - $low) / $delta * 255, 0, 255); $cols['green'] = \TYPO3\CMS\Core\Utility\MathUtility::forceIntegerInRange(($cols['green'] - $low) / $delta * 255, 0, 255); $cols['blue'] = \TYPO3\CMS\Core\Utility\MathUtility::forceIntegerInRange(($cols['blue'] - $low) / $delta * 255, 0, 255); ImageColorSet($im, $c, $cols['red'], $cols['green'], $cols['blue']); } } }
/** * Add Copyright text to thumbnail * */ private function addcopyright() { if ($this->Copyrightfonttype=='') { $widthx=imagefontwidth($this->Copyrightfontsize)*strlen($this->Copyrighttext); $heighty=imagefontheight($this->Copyrightfontsize); $fontwidth=imagefontwidth($this->Copyrightfontsize); } else { $dimensions=imagettfbbox($this->Copyrightfontsize,0,$this->Copyrightfonttype,$this->Copyrighttext); $widthx=$dimensions[2];$heighty=$dimensions[5]; $dimensions=imagettfbbox($this->Copyrightfontsize,0,$this->Copyrightfonttype,'W'); $fontwidth=$dimensions[2]; } $cpos=explode(' ',str_replace('%','',$this->Copyrightposition)); if (count($cpos)>1) { $cposx=floor(min(max($this->thumbx*($cpos[0]/100)-0.5*$widthx,$fontwidth),$this->thumbx-$widthx-0.5*$fontwidth)); $cposy=floor(min(max($this->thumby*($cpos[1]/100)-0.5*$heighty,$heighty),$this->thumby-$heighty*1.5)); } else { $cposx=$fontwidth; $cposy=$this->thumby-10; } if ($this->Copyrighttextcolor=='') { $colors=array(); for ($i=$cposx;$i<($cposx+$widthx);$i++) { $indexis=ImageColorAt($this->thumb,$i,$cposy+0.5*$heighty); $rgbarray=ImageColorsForIndex($this->thumb,$indexis); array_push($colors,$rgbarray['red'],$rgbarray['green'],$rgbarray['blue']); } if (array_sum($colors)/count($colors)>180) { if ($this->Copyrightfonttype=='') imagestring($this->thumb,$this->Copyrightfontsize,$cposx,$cposy,$this->Copyrighttext,imagecolorallocate($this->thumb,0,0,0)); else imagettftext($this->thumb,$this->Copyrightfontsize,0,$cposx,$cposy,imagecolorallocate($this->thumb,0,0,0),$this->Copyrightfonttype,$this->Copyrighttext); } else { if ($this->Copyrightfonttype=='') imagestring($this->thumb,$this->Copyrightfontsize,$cposx,$cposy,$this->Copyrighttext,imagecolorallocate($this->thumb,255,255,255)); else imagettftext($this->thumb,$this->Copyrightfontsize,0,$cposx,$cposy,imagecolorallocate($this->thumb,255,255,255),$this->Copyrightfonttype,$this->Copyrighttext); } } else { if ($this->Copyrightfonttype=='') imagestring($this->thumb,$this->Copyrightfontsize,$cposx,$cposy,$this->Copyrighttext,imagecolorallocate($this->thumb,hexdec(substr($this->Copyrighttextcolor,1,2)),hexdec(substr($this->Copyrighttextcolor,3,2)),hexdec(substr($this->Copyrighttextcolor,5,2)))); else imagettftext($this->thumb,$this->Copyrightfontsize,0,$cposx,$cposy,imagecolorallocate($this->thumb,hexdec(substr($this->Copyrighttextcolor,1,2)),hexdec(substr($this->Copyrighttextcolor,3,2)),hexdec(substr($this->Copyrighttextcolor,5,2))),$this->Copyrightfonttype,$this->Copyrighttext); } }
/** * resizes an image using several different techniques: * * PHP's own ImageCopyResamplated * Bi-linear filter (slower, but better quality than ImageCopyResampled) * Bi-Cubic filter (slowest, but offers the best quality) * PHP's own ImageCopyResized (fastest one, but offers no antialising or filter) * */ function ImageResize($dst_img, &$src_img, $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h, $resample = GD_RESIZER_NO_SMOOTHING_MODE) { $pxls = intval($src_w / $dst_w) - 1; if ($resample == GD_RESIZER_PHP_IMAGECOPYRESAMPLED) { imagecopyresampled($dst_img, $src_img, $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h); } elseif ($resample == GD_RESIZER_BILINEAR_MODE) { //slow but better quality ImageTrueColorToPalette($src_img, false, 256); ImagePaletteCopy($dst_img, $src_img); $rX = $src_w / $dst_w; $rY = $src_h / $dst_h; $nY = 0; for ($y = $src_y; $y < $dst_h; $y++) { $oY = $nY; $nY = intval(($y + 1) * $rY + 0.5); $nX = 0; for ($x = $src_x; $x < $dst_w; $x++) { $r = $g = $b = $a = 0; $oX = $nX; $nX = intval(($x + 1) * $rX + 0.5); $c = ImageColorsForIndex($src_img, ImageColorAt($src_img, $nX, $nY)); $r += $c['red']; $g += $c['green']; $b += $c['blue']; $a++; $c = ImageColorsForIndex($src_img, ImageColorAt($src_img, $nX - $pxls, $nY - $pxls)); $r += $c['red']; $g += $c['green']; $b += $c['blue']; $a++; //you can add more pixels here! eg "$nX, $nY-$pxls" or "$nX-$pxls, $nY" ImageSetPixel($dst_img, $x + $dst_x - $src_x, $y + $dst_y - $src_y, ImageColorClosest($dst_img, $r / $a, $g / $a, $b / $a)); } } } elseif ($resample == GD_RESIZER_BICUBIC_MODE) { // veeeeeery slow but better quality ImagePaletteCopy($dst_img, $src_img); $rX = $src_w / $dst_w; $rY = $src_h / $dst_h; $nY = 0; for ($y = $src_y; $y < $dst_h; $y++) { $oY = $nY; $nY = intval(($y + 1) * $rY + 0.5); $nX = 0; for ($x = $src_x; $x < $dst_w; $x++) { $r = $g = $b = $a = 0; $oX = $nX; $nX = intval(($x + 1) * $rX + 0.5); for ($i = $nY; --$i >= $oY;) { for ($j = $nX; --$j >= $oX;) { $c = ImageColorsForIndex($src_img, ImageColorAt($src_img, $j, $i)); $r += $c['red']; $g += $c['green']; $b += $c['blue']; $a++; } } ImageSetPixel($dst_img, $x + $dst_x - $src_x, $y + $dst_y - $src_y, ImageColorClosest($dst_img, $r / $a, $g / $a, $b / $a)); } } } else { $dst_w++; $dst_h++; //->no black border imagecopyresized($dst_img, $src_img, $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h); } }
function ImageCopyResampleBicubic($dst_img, $src_img, $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h) { // ron at korving dot demon dot nl // http://www.php.net/imagecopyresampled $scaleX = ($src_w - 1) / $dst_w; $scaleY = ($src_h - 1) / $dst_h; $scaleX2 = $scaleX / 2.0; $scaleY2 = $scaleY / 2.0; $isTrueColor = ImageIsTrueColor($src_img); for ($y = $src_y; $y < $src_y + $dst_h; $y++) { $sY = $y * $scaleY; $siY = (int) $sY; $siY2 = (int) $sY + $scaleY2; for ($x = $src_x; $x < $src_x + $dst_w; $x++) { $sX = $x * $scaleX; $siX = (int) $sX; $siX2 = (int) $sX + $scaleX2; if ($isTrueColor) { $c1 = ImageColorAt($src_img, $siX, $siY2); $c2 = ImageColorAt($src_img, $siX, $siY); $c3 = ImageColorAt($src_img, $siX2, $siY2); $c4 = ImageColorAt($src_img, $siX2, $siY); $r = $c1 + $c2 + $c3 + $c4 >> 2 & 0xff0000; $g = ($c1 & 0xff00) + ($c2 & 0xff00) + ($c3 & 0xff00) + ($c4 & 0xff00) >> 2 & 0xff00; $b = ($c1 & 0xff) + ($c2 & 0xff) + ($c3 & 0xff) + ($c4 & 0xff) >> 2; } else { $c1 = ImageColorsForIndex($src_img, ImageColorAt($src_img, $siX, $siY2)); $c2 = ImageColorsForIndex($src_img, ImageColorAt($src_img, $siX, $siY)); $c3 = ImageColorsForIndex($src_img, ImageColorAt($src_img, $siX2, $siY2)); $c4 = ImageColorsForIndex($src_img, ImageColorAt($src_img, $siX2, $siY)); $r = $c1['red'] + $c2['red'] + $c3['red'] + $c4['red'] << 14; $g = $c1['green'] + $c2['green'] + $c3['green'] + $c4['green'] << 6; $b = $c1['blue'] + $c2['blue'] + $c3['blue'] + $c4['blue'] >> 2; } ImageSetPixel($dst_img, $dst_x + $x - $src_x, $dst_y + $y - $src_y, $r + $g + $b); } } return true; }
function ImageResize(&$src, $x, $y) { $dst = imagecreatetruecolor($x, $y); $pals = ImageColorsTotal($src); for ($i = 0; $i < $pals; $i++) { $colors = ImageColorsForIndex($src, $i); ImageColorAllocate($dst, $colors['red'], $colors['green'], $colors['blue']); } $scX = (imagesx($src) - 1) / $x; $scY = (imagesy($src) - 1) / $y; $scX2 = intval($scX / 2); $scY2 = intval($scY / 2); for ($j = 0; $j < $y; $j++) { $sY = intval($j * $scY); $y13 = $sY + $scY2; for ($i = 0; $i < $x; $i++) { $sX = intval($i * $scX); $x34 = $sX + $scX2; $c1 = ImageColorsForIndex($src, ImageColorAt($src, $sX, $y13)); $c2 = ImageColorsForIndex($src, ImageColorAt($src, $sX, $sY)); $c3 = ImageColorsForIndex($src, ImageColorAt($src, $x34, $y13)); $c4 = ImageColorsForIndex($src, ImageColorAt($src, $x34, $sY)); $r = ($c1['red'] + $c2['red'] + $c3['red'] + $c4['red']) / 4; $g = ($c1['green'] + $c2['green'] + $c3['green'] + $c4['green']) / 4; $b = ($c1['blue'] + $c2['blue'] + $c3['blue'] + $c4['blue']) / 4; ImageSetPixel($dst, $i, $j, ImageColorClosest($dst, $r, $g, $b)); } } return $dst; }
/** * @param $dst_img * @param $src_img * @param $dst_x * @param $dst_y * @param $src_x * @param $src_y * @param $dst_w * @param $dst_h * @param $src_w * @param $src_h */ function ImageCopyResampleBicubic(&$dst_img, &$src_img, $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h) { $palsize = ImageColorsTotal($src_img); for ($i = 0; $i < $palsize; $i++) { // get palette. $colors = ImageColorsForIndex($src_img, $i); ImageColorAllocate($dst_img, $colors['red'], $colors['green'], $colors['blue']); } $scaleX = ($src_w - 1) / $dst_w; $scaleY = ($src_h - 1) / $dst_h; $scaleX2 = (int) ($scaleX / 2); $scaleY2 = (int) ($scaleY / 2); for ($j = $src_y; $j < $dst_h; $j++) { $sY = (int) ($j * $scaleY); $y13 = $sY + $scaleY2; for ($i = $src_x; $i < $dst_w; $i++) { $sX = (int) ($i * $scaleX); $x34 = $sX + $scaleX2; $color1 = ImageColorsForIndex($src_img, ImageColorAt($src_img, $sX, $y13)); $color2 = ImageColorsForIndex($src_img, ImageColorAt($src_img, $sX, $sY)); $color3 = ImageColorsForIndex($src_img, ImageColorAt($src_img, $x34, $y13)); $color4 = ImageColorsForIndex($src_img, ImageColorAt($src_img, $x34, $sY)); $red = ($color1['red'] + $color2['red'] + $color3['red'] + $color4['red']) / 4; $green = ($color1['green'] + $color2['green'] + $color3['green'] + $color4['green']) / 4; $blue = ($color1['blue'] + $color2['blue'] + $color3['blue'] + $color4['blue']) / 4; ImageSetPixel($dst_img, $i + $dst_x - $src_x, $j + $dst_y - $src_y, ImageColorClosest($dst_img, $red, $green, $blue)); } } }
/** * Apply input levels to input image pointer (increasing contrast) * * @param integer GDlib Image Pointer * @param integer The "low" value (close to 0) * @param integer The "high" value (close to 255) * @param boolean If swap, then low and high are swapped. (Useful for negated masks...) * @return void */ function inputLevels(&$im, $low, $high, $swap = '') { if ($low < $high) { $low = t3lib_div::intInRange($low, 0, 255); $high = t3lib_div::intInRange($high, 0, 255); if ($swap) { $temp = $low; $low = 255 - $high; $high = 255 - $temp; } $delta = $high - $low; $totalCols = ImageColorsTotal($im); for ($c = 0; $c < $totalCols; $c++) { $cols = ImageColorsForIndex($im, $c); $cols['red'] = t3lib_div::intInRange(($cols['red'] - $low) / $delta * 255, 0, 255); $cols['green'] = t3lib_div::intInRange(($cols['green'] - $low) / $delta * 255, 0, 255); $cols['blue'] = t3lib_div::intInRange(($cols['blue'] - $low) / $delta * 255, 0, 255); ImageColorSet($im, $c, $cols['red'], $cols['green'], $cols['blue']); } } }
/** * Creates the icon file for the function getIcon() * * @param string $iconfile Original unprocessed Icon file, relative path to PATH_typo3 * @param string $mode Mode string, eg. "deleted" or "futuretiming" determining how the icon will look * @param integer $user The number of the fe_group record uid if applicable * @param boolean $protectSection Flag determines if the protected-section icon should be applied. * @param string $absFile Absolute path to file from which to create the icon. * @param string $iconFileName_stateTagged The filename that this icon should have had, basically [icon base name]_[flags].[extension] - used for part of temporary filename * @return string Filename relative to PATH_typo3 * @access private */ public static function makeIcon($iconfile, $mode, $user, $protectSection, $absFile, $iconFileName_stateTagged) { $iconFileName = 'icon_' . GeneralUtility::shortMD5($iconfile . '|' . $mode . '|-' . $user . '|' . $protectSection) . '_' . $iconFileName_stateTagged . '.' . ($GLOBALS['TYPO3_CONF_VARS']['GFX']['gdlib_png'] ? 'png' : 'gif'); $mainpath = '../typo3temp/' . $iconFileName; $path = PATH_site . 'typo3temp/' . $iconFileName; if (file_exists(PATH_typo3 . 'icons/' . $iconFileName)) { // Returns if found in typo3/icons/ return 'icons/' . $iconFileName; } elseif (file_exists($path)) { // Returns if found in ../typo3temp/icons/ return $mainpath; } else { // Makes icon: if (file_exists($absFile)) { if ($GLOBALS['TYPO3_CONF_VARS']['GFX']['gdlib']) { // Create image pointer, if possible $im = self::imagecreatefrom($absFile); if ($im < 0) { return $iconfile; } // Converting to gray scale, dimming the icon: if ($mode == 'disabled' or $mode != 'futuretiming' && $mode != 'no_icon_found' && !(!$mode && $user)) { $totalImageColors = ImageColorsTotal($im); for ($c = 0; $c < $totalImageColors; $c++) { $cols = ImageColorsForIndex($im, $c); $newcol = round(($cols['red'] + $cols['green'] + $cols['blue']) / 3); $lighten = $mode == 'disabled' ? 2.5 : 2; $newcol = round(255 - (255 - $newcol) / $lighten); ImageColorSet($im, $c, $newcol, $newcol, $newcol); } } // Applying user icon, if there are access control on the item: if ($user) { if ($user < 100) { // Apply user number only if lower than 100 $black = ImageColorAllocate($im, 0, 0, 0); imagefilledrectangle($im, 0, 0, $user > 10 ? 9 : 5, 8, $black); $white = ImageColorAllocate($im, 255, 255, 255); imagestring($im, 1, 1, 1, $user, $white); } $ol_im = self::imagecreatefrom($GLOBALS['BACK_PATH'] . 'gfx/overlay_group.gif'); if ($ol_im < 0) { return $iconfile; } self::imagecopyresized($im, $ol_im, 0, 0, 0, 0, imagesx($ol_im), imagesy($ol_im), imagesx($ol_im), imagesy($ol_im)); } // Applying overlay based on mode: if ($mode) { unset($ol_im); switch ($mode) { case 'deleted': $ol_im = self::imagecreatefrom($GLOBALS['BACK_PATH'] . 'gfx/overlay_deleted.gif'); break; case 'futuretiming': $ol_im = self::imagecreatefrom($GLOBALS['BACK_PATH'] . 'gfx/overlay_timing.gif'); break; case 'timing': $ol_im = self::imagecreatefrom($GLOBALS['BACK_PATH'] . 'gfx/overlay_timing.gif'); break; case 'hiddentiming': $ol_im = self::imagecreatefrom($GLOBALS['BACK_PATH'] . 'gfx/overlay_hidden_timing.gif'); break; case 'no_icon_found': $ol_im = self::imagecreatefrom($GLOBALS['BACK_PATH'] . 'gfx/overlay_no_icon_found.gif'); break; case 'disabled': // is already greyed - nothing more $ol_im = 0; break; case 'hidden': default: $ol_im = self::imagecreatefrom($GLOBALS['BACK_PATH'] . 'gfx/overlay_hidden.gif'); } if ($ol_im < 0) { return $iconfile; } if ($ol_im) { self::imagecopyresized($im, $ol_im, 0, 0, 0, 0, imagesx($ol_im), imagesy($ol_im), imagesx($ol_im), imagesy($ol_im)); } } // Protect-section icon: if ($protectSection) { $ol_im = self::imagecreatefrom($GLOBALS['BACK_PATH'] . 'gfx/overlay_sub5.gif'); if ($ol_im < 0) { return $iconfile; } self::imagecopyresized($im, $ol_im, 0, 0, 0, 0, imagesx($ol_im), imagesy($ol_im), imagesx($ol_im), imagesy($ol_im)); } // Create the image as file, destroy GD image and return: @self::imagemake($im, $path); GeneralUtility::gif_compress($path, 'IM'); ImageDestroy($im); return $mainpath; } else { return $iconfile; } } else { return $GLOBALS['BACK_PATH'] . 'gfx/fileicons/default.gif'; } } }
function GetPixelColor(&$img, $x, $y) { return @ImageColorsForIndex($img, @ImageColorAt($img, $x, $y)); }
/** * alternative workaround function for bicubic resizing. works well for downsizing and upsizing. VERY VERY slow. taken from php.net comments * * @access private */ function _imageCopyResampledWorkaround2(&$dst_img, &$src_img, $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h) { ImagePaletteCopy($dst_img, $src_img); $rX = $src_w / $dst_w; $rY = $src_h / $dst_h; $w = 0; for ($y = $dst_y; $y < $dst_h; $y++) { $ow = $w; $w = round(($y + 1) * $rY); $t = 0; for ($x = $dst_x; $x < $dst_w; $x++) { $r = $g = $b = 0; $a = 0; $ot = $t; $t = round(($x + 1) * $rX); for ($u = 0; $u < $w - $ow; $u++) { for ($p = 0; $p < $t - $ot; $p++) { $c = ImageColorsForIndex($src_img, ImageColorAt($src_img, $ot + $p, $ow + $u)); $r += $c['red']; $g += $c['green']; $b += $c['blue']; $a++; } } ImageSetPixel($dst_img, $x, $y, ImageColorClosest($dst_img, $r / $a, $g / $a, $b / $a)); } } }
static function _build_icon($source, $target, $newcolor) { $rgb = array('r' => hexdec(substr($newcolor, 1, 2)), 'g' => hexdec(substr($newcolor, 3, 2)), 'b' => hexdec(substr($newcolor, 5, 2))); $img = imagecreatefromgif(sys_custom($source)); // greyscale for ($i = 0; $i < imagecolorstotal($img); $i++) { $c = ImageColorsForIndex($img, $i); $t = ($c["red"] + $c["green"] + $c["blue"]) / 3; imagecolorset($img, $i, $t, $t, $t); } $red = floor(($rgb["r"] + 30) / 2.55); $green = floor(($rgb["g"] + 30) / 2.55); $blue = floor(($rgb["b"] + 30) / 2.55); for ($i = 0; $i < imagecolorstotal($img); $i++) { $col = ImageColorsForIndex($img, $i); $red_set = $red / 100 * $col['red']; $green_set = $green / 100 * $col['green']; $blue_set = $blue / 100 * $col['blue']; if ($red_set > 255) { $red_set = 255; } if ($green_set > 255) { $green_set = 255; } if ($blue_set > 255) { $blue_set = 255; } imagecolorset($img, $i, $red_set, $green_set, $blue_set); } imagegif($img, $target); }