function changeBackground($im, $red, $green, $blue) { imagetruecolortopalette($im, false, 255); $ig = imagecolorat($im, 0, 0); imagecolorset($im, $ig, $red, $green, $blue); return $im; }
/** * Fit small image to specified bound * * @param string $src * @param string $dest * @param int $width * @param int $height * @return bool */ public function fit($src, $dest, $width, $height) { // Calculate $size = getimagesize($src); $ratio = max($width / $size[0], $height / $size[1]); $old_width = $size[0]; $old_height = $size[1]; $new_width = intval($old_width * $ratio); $new_height = intval($old_height * $ratio); // Resize @ini_set('memory_limit', apply_filters('image_memory_limit', WP_MAX_MEMORY_LIMIT)); $image = imagecreatefromstring(file_get_contents($src)); $new_image = wp_imagecreatetruecolor($new_width, $new_height); imagecopyresampled($new_image, $image, 0, 0, 0, 0, $new_width, $new_height, $old_width, $old_height); if (IMAGETYPE_PNG == $size[2] && function_exists('imageistruecolor') && !imageistruecolor($image)) { imagetruecolortopalette($new_image, false, imagecolorstotal($image)); } // Destroy old image imagedestroy($image); // Save switch ($size[2]) { case IMAGETYPE_GIF: $result = imagegif($new_image, $dest); break; case IMAGETYPE_PNG: $result = imagepng($new_image, $dest); break; default: $result = imagejpeg($new_image, $dest); break; } imagedestroy($new_image); return $result; }
function execute() { $img =& $this->image->getImage(); if (!($t = imagecolorstotal($img))) { $t = 256; imagetruecolortopalette($img, true, $t); } $total = imagecolorstotal($img); for ($i = 0; $i < $total; $i++) { $index = imagecolorsforindex($img, $i); $red = $index["red"] * 0.393 + $index["green"] * 0.769 + $index["blue"] * 0.189; $green = $index["red"] * 0.349 + $index["green"] * 0.6860000000000001 + $index["blue"] * 0.168; $blue = $index["red"] * 0.272 + $index["green"] * 0.534 + $index["blue"] * 0.131; if ($red > 255) { $red = 255; } if ($green > 255) { $green = 255; } if ($blue > 255) { $blue = 255; } imagecolorset($img, $i, $red, $green, $blue); } }
/** * Reduces colors of a given image * * @param \Intervention\Image\Image $image * @return boolean */ public function execute($image) { $count = $this->argument(0)->value(); $matte = $this->argument(1)->value(); // get current image size $size = $image->getSize(); // create empty canvas $resource = imagecreatetruecolor($size->width, $size->height); // define matte if (is_null($matte)) { $matte = imagecolorallocatealpha($resource, 255, 255, 255, 127); } else { $matte = $image->getDriver()->parseColor($matte)->getInt(); } // fill with matte and copy original image imagefill($resource, 0, 0, $matte); // set transparency imagecolortransparent($resource, $matte); // copy original image imagecopy($resource, $image->getCore(), 0, 0, 0, 0, $size->width, $size->height); if (is_numeric($count) && $count <= 256) { // decrease colors imagetruecolortopalette($resource, true, $count); } // set new resource $image->setCore($resource); return true; }
private function create_new_rsrc($mimetype, $width, $height) { switch ($mimetype) { case 'image/jpeg': $_rsrc = imagecreatetruecolor($width, $height); return $_rsrc; case 'image/png': // from supersizer $_rsrc = imagecreatetruecolor($width, $height); $color = imagecolorallocatealpha($_rsrc, 0, 0, 0, 127); imagecolortransparent($_rsrc, $color); $this->_transparent = $color; return $_rsrc; case 'image/gif': $_rsrc = imagecreatetruecolor($width, $height); imagetruecolortopalette($_rsrc, true, 256); imagealphablending($_rsrc, false); imagesavealpha($_rsrc, true); $transparent = imagecolorallocatealpha($_rsrc, 255, 255, 255, 127); imagefilledrectangle($_rsrc, 0, 0, $width, $height, $transparent); imagecolortransparent($_rsrc, $transparent); return $_rsrc; default: throw new Exception('Cannot create new image of type ' . $mimetype); } }
/** * Output an image. If the image is true-color, it will be converted * to a paletted image first using imagetruecolortopalette(). * * @param resource handle * @return bool */ public function output($handle) { if (imageistruecolor($handle)) { imagetruecolortopalette($handle, $this->dither, $this->ncolors); } return imagegif($handle); }
public function execute() { $this->media->asImage(); $img = $this->media->getImage(); if (!($t = imagecolorstotal($img))) { $t = 256; imagetruecolortopalette($img, true, $t); } $total = imagecolorstotal($img); for ($i = 0; $i < $total; ++$i) { $index = imagecolorsforindex($img, $i); $red = $index['red'] * 0.393 + $index['green'] * 0.769 + $index['blue'] * 0.189; $green = $index['red'] * 0.349 + $index['green'] * 0.6860000000000001 + $index['blue'] * 0.168; $blue = $index['red'] * 0.272 + $index['green'] * 0.534 + $index['blue'] * 0.131; if ($red > 255) { $red = 255; } if ($green > 255) { $green = 255; } if ($blue > 255) { $blue = 255; } imagecolorset($img, $i, $red, $green, $blue); } $this->media->setImage($img); }
/** * Convert an image. Returns TRUE when successfull, FALSE if image is * not a truecolor image. * * @param img.Image image * @return bool * @throws img.ImagingException */ public function convert($image) { if (!imageistruecolor($image->handle)) { return FALSE; } return imagetruecolortopalette($image->handle, $this->dither, $this->ncolors); }
public function run($file) { $res = $this->open_image($file); if ($res != TRUE) { return FALSE; } $this->image_progressive = isset($this->settings['field_settings']['progressive_jpeg']) === TRUE && $this->settings['field_settings']['progressive_jpeg'] == 'yes' ? TRUE : FALSE; $this->Ageimage = array(1, 0, 60); imagetruecolortopalette($this->EE->channel_images->image, 1, 256); for ($c = 0; $c < 256; $c++) { $col = imagecolorsforindex($this->EE->channel_images->image, $c); $new_col = floor($col['red'] * 0.2125 + $col['green'] * 0.7154 + $col['blue'] * 0.0721); $noise = rand(-$this->Ageimage[1], $this->Ageimage[1]); if ($this->Ageimage[2] > 0) { $r = $new_col + $this->Ageimage[2] + $noise; $g = floor($new_col + $this->Ageimage[2] / 1.86 + $noise); $b = floor($new_col + $this->Ageimage[2] / -3.48 + $noise); } else { $r = $new_col + $noise; $g = $new_col + $noise; $b = $new_col + $noise; } imagecolorset($this->EE->channel_images->image, $c, max(0, min(255, $r)), max(0, min(255, $g)), max(0, min(255, $b))); } $this->save_image($file); return TRUE; }
/** * @return ZipInterface */ public function render() { $pathThumbnail = $this->getPresentation()->getPresentationProperties()->getThumbnailPath(); if ($pathThumbnail) { // Size : 128x128 pixel // PNG : 8bit, non-interlaced with full alpha transparency $gdImage = imagecreatefromstring(file_get_contents($pathThumbnail)); if ($gdImage) { list($width, $height) = getimagesize($pathThumbnail); $gdRender = imagecreatetruecolor(128, 128); $colorBgAlpha = imagecolorallocatealpha($gdRender, 0, 0, 0, 127); imagecolortransparent($gdRender, $colorBgAlpha); imagefill($gdRender, 0, 0, $colorBgAlpha); imagecopyresampled($gdRender, $gdImage, 0, 0, 0, 0, 128, 128, $width, $height); imagetruecolortopalette($gdRender, false, 255); imagesavealpha($gdRender, true); ob_start(); imagepng($gdRender); $imageContents = ob_get_contents(); ob_end_clean(); imagedestroy($gdRender); imagedestroy($gdImage); $this->getZip()->addFromString('Thumbnails/thumbnail.png', $imageContents); } } return $this->getZip(); }
/** * Convert the image to 2 colours with dithering. */ protected function dither() { if (!imageistruecolor($this->image)) { imagepalettetotruecolor($this->image); } imagefilter($this->image, IMG_FILTER_GRAYSCALE); imagetruecolortopalette($this->image, true, 2); }
/** * @param resource $resource * @return resource */ public function getPalettizedGdResource($resource) { imagetruecolortopalette($resource, true, 255); if (-1 == ($trans = imagecolortransparent($resource))) { $trans = imagecolorallocate($resource, 255, 255, 255); imagecolortransparent($resource, $trans); } return $resource; }
/** * This function is almost equal to the image_resize (native function of wordpress) */ function image_resize($file, $max_w, $max_h, $crop = false, $far = false, $iar = false, $dest_path = null, $jpeg_quality = 90) { $image = wp_load_image($file); if (!is_resource($image)) { return new WP_Error('error_loading_image', $image); } $size = @getimagesize($file); if (!$size) { return new WP_Error('invalid_image', __('Could not read image size'), $file); } list($orig_w, $orig_h, $orig_type) = $size; $dims = mf_image_resize_dimensions($orig_w, $orig_h, $max_w, $max_h, $crop, $far, $iar); if (!$dims) { $dims = array(0, 0, 0, 0, $orig_w, $orig_h, $orig_w, $orig_h); } list($dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h) = $dims; $newimage = imagecreatetruecolor($dst_w, $dst_h); imagealphablending($newimage, false); imagesavealpha($newimage, true); $transparent = imagecolorallocatealpha($newimage, 255, 255, 255, 127); imagefilledrectangle($newimage, 0, 0, $dst_w, $dst_h, $transparent); imagecopyresampled($newimage, $image, $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h); // convert from full colors to index colors, like original PNG. if (IMAGETYPE_PNG == $orig_type && !imageistruecolor($image)) { imagetruecolortopalette($newimage, false, imagecolorstotal($image)); } // we don't need the original in memory anymore imagedestroy($image); $info = pathinfo($dest_path); $dir = $info['dirname']; $ext = $info['extension']; $name = basename($dest_path, ".{$ext}"); $destfilename = "{$dir}/{$name}.{$ext}"; if (IMAGETYPE_GIF == $orig_type) { if (!imagegif($newimage, $destfilename)) { return new WP_Error('resize_path_invalid', __('Resize path invalid')); } } elseif (IMAGETYPE_PNG == $orig_type) { if (!imagepng($newimage, $destfilename)) { return new WP_Error('resize_path_invalid', __('Resize path invalid')); } } else { // all other formats are converted to jpg //Todo: add option for use progresive JPG //imageinterlace($newimage, true); //Progressive JPG if (!imagejpeg($newimage, $destfilename, apply_filters('jpeg_quality', $jpeg_quality, 'image_resize'))) { return new WP_Error('resize_path_invalid', __('Resize path invalid')); } } imagedestroy($newimage); // Set correct file permissions $stat = stat(dirname($destfilename)); $perms = $stat['mode'] & 0666; //same permissions as parent folder, strip off the executable bits @chmod($destfilename, $perms); return $destfilename; }
protected function _save($image, $filename = null, $mime_type = null) { global $ewww_debug; if (!defined('EWWW_IMAGE_OPTIMIZER_DOMAIN')) { require_once plugin_dir_path(__FILE__) . 'ewww-image-optimizer.php'; } if (!defined('EWWW_IMAGE_OPTIMIZER_JPEGTRAN')) { ewww_image_optimizer_init(); } list($filename, $extension, $mime_type) = $this->get_output_format($filename, $mime_type); if (!$filename) { $filename = $this->generate_filename(null, null, $extension); } if ('image/gif' == $mime_type) { if (!$this->make_image($filename, 'imagegif', array($image, $filename))) { return new WP_Error('image_save_error', __('Image Editor Save Failed')); } } elseif ('image/png' == $mime_type) { // convert from full colors to index colors, like original PNG. if (function_exists('imageistruecolor') && !imageistruecolor($image)) { imagetruecolortopalette($image, false, imagecolorstotal($image)); } if (property_exists('WP_Image_Editor', 'quality')) { $compression_level = floor((101 - $this->quality) * 0.09); $ewww_debug .= "png quality = " . $this->quality . "<br>"; } else { $compression_level = floor((101 - false) * 0.09); } if (!$this->make_image($filename, 'imagepng', array($image, $filename, $compression_level))) { return new WP_Error('image_save_error', __('Image Editor Save Failed')); } } elseif ('image/jpeg' == $mime_type) { if (method_exists($this, 'get_quality')) { if (!$this->make_image($filename, 'imagejpeg', array($image, $filename, $this->get_quality()))) { return new WP_Error('image_save_error', __('Image Editor Save Failed')); } } else { if (!$this->make_image($filename, 'imagejpeg', array($image, $filename, apply_filters('jpeg_quality', $this->quality, 'image_resize')))) { return new WP_Error('image_save_error', __('Image Editor Save Failed')); } } } else { return new WP_Error('image_save_error', __('Image Editor Save Failed')); } // Set correct file permissions $stat = stat(dirname($filename)); $perms = $stat['mode'] & 0666; //same permissions as parent folder, strip off the executable bits @chmod($filename, $perms); ewww_image_optimizer_aux_images_loop($filename, true); $ewww_debug = "{$ewww_debug} image editor (gd) saved: {$filename} <br>"; $image_size = filesize($filename); $ewww_debug = "{$ewww_debug} image editor size: {$image_size} <br>"; ewww_image_optimizer_debug_log(); return array('path' => $filename, 'file' => wp_basename(apply_filters('image_make_intermediate_size', $filename)), 'width' => $this->size['width'], 'height' => $this->size['height'], 'mime-type' => $mime_type); }
function build_ycon($filename, $seed = '', $size = '') { $hash = md5($seed); $image = ycon($hash, $size, 255, 255, 255); imagetruecolortopalette($image, false, 64); // header('Content-type: image/png'); imagepng($image, $filename); imagedestroy($image); return true; }
/** * Workaround method for restoring alpha transparency for gif images * * @param resource $src * @param resource $dest * @return resource transparent gf color */ public static function restoreGifAlphaColor(&$src, &$dest) { $transparentcolor = imagecolortransparent($src); if ($transparentcolor != -1) { $colorcount = imagecolorstotal($src); imagetruecolortopalette($dest, true, $colorcount); imagepalettecopy($dest, $src); imagefill($dest, 0, 0, $transparentcolor); imagecolortransparent($dest, $transparentcolor); } return $transparentcolor; }
/** * Convert an image. Returns TRUE when successfull, FALSE if image is * not a truecolor image. * * @param img.Image image * @return bool * @throws img.ImagingException */ public function convert($image) { if (!imageistruecolor($image->handle)) { return false; } $tmp = Image::create($image->getWidth(), $image->getHeight(), IMG_TRUECOLOR); $tmp->copyFrom($image); imagetruecolortopalette($image->handle, $this->dither, $this->ncolors); imagecolormatch($tmp->handle, $image->handle); unset($tmp); return true; }
function uploadImageAs8BitPNG($data, $destination, $width, $height) { $srcimage = imagecreatefromstring($data); $img = imagecreatetruecolor($width, $height); $bga = imagecolorallocatealpha($img, 0, 0, 0, 127); imagecolortransparent($img, $bga); imagefill($img, 0, 0, $bga); imagecopy($img, $srcimage, 0, 0, 0, 0, $width, $height); imagetruecolortopalette($img, false, 255); imagesavealpha($img, true); imagepng($img, $destination); imagedestroy($img); }
private function _preserveAlpha($image) { if ($this->format == 'png' && $this->options['preserveAlpha'] === true) { imagealphablending($image, false); imagefill($image, 0, 0, imagecolorallocatealpha($image, $this->options['alphaMaskColor'][0], $this->options['alphaMaskColor'][1], $this->options['alphaMaskColor'][2], 0)); imagesavealpha($image, true); } if ($this->format == 'gif' && $this->options['preserveTransparency'] === true) { imagecolortransparent($image, imagecolorallocate($image, $this->options['transparencyMaskColor'][0], $this->options['transparencyMaskColor'][1], $this->options['transparencyMaskColor'][2])); imagetruecolortopalette($image, true, 256); } return $image; }
public function resize($width, $height, $keepRatio, $file, $target, $keepSmaller = true, $cropToFit = false, $jpegQuality = 75, $pngQuality = 6, $png8Bits = false) { list($oldWidth, $oldHeight, $type) = getimagesize($file); $i = getimagesize($file); switch ($type) { case IMAGETYPE_PNG: $source = imagecreatefrompng($file); break; case IMAGETYPE_JPEG: $source = imagecreatefromjpeg($file); break; case IMAGETYPE_GIF: $source = imagecreatefromgif($file); break; } $srcX = $srcY = 0; if ($cropToFit) { list($srcX, $srcY, $oldWidth, $oldHeight) = $this->_calculateSourceRectangle($oldWidth, $oldHeight, $width, $height); } elseif (!$keepSmaller || $oldWidth > $width || $oldHeight > $height) { if ($keepRatio) { list($width, $height) = $this->_calculateWidth($oldWidth, $oldHeight, $width, $height); } } else { $width = $oldWidth; $height = $oldHeight; } if (imageistruecolor($source) == false || $type == IMAGETYPE_GIF) { $thumb = imagecreate($width, $height); } else { $thumb = imagecreatetruecolor($width, $height); } imagealphablending($thumb, false); imagesavealpha($thumb, true); imagecopyresampled($thumb, $source, 0, 0, $srcX, $srcY, $width, $height, $oldWidth, $oldHeight); if ($type == IMAGETYPE_PNG && $png8Bits === true) { imagetruecolortopalette($thumb, true, 255); } switch ($type) { case IMAGETYPE_PNG: imagepng($thumb, $target, $pngQuality); break; case IMAGETYPE_JPEG: imagejpeg($thumb, $target, $jpegQuality); break; case IMAGETYPE_GIF: imagegif($thumb, $target); break; } imagedestroy($thumb); return $target; }
function img_downsize($old_fn, $new_fn, $max_w, $max_h, $crop = 0, $q = 75, $interlace = 0) { list($old_w, $old_h, $type) = getimagesize($old_fn); // Make sure we have enough memory if the image is large if (max($old_w, $old_h) > 1024) { // this won't work on all servers but it's worth a try ini_set('memory_limit', EXTRA_MEMORY); } $old_img = null; if ($type == 1) { $old_img = imagecreatefromgif($old_fn); } elseif ($type == 2) { $old_img = imagecreatefromjpeg($old_fn); } elseif ($type == 3) { $old_img = imagecreatefrompng($old_fn); } if (!$old_img) { trigger_error('error_loading_image', array('{file}' => $old_fn)); return false; } $newsize = img_newsize($old_w, $old_h, $max_w, $max_h, $crop); if (!$newsize) { trigger_error('invalid_size', array('{w}' => $max_w, '{h}' => $max_h)); return false; } list($dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h) = $newsize; $new_img = imagecreatetruecolor($dst_w, $dst_h); if (imagecopyresampled($new_img, $old_img, $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h)) { $r = false; if (file_exists($new_fn)) { unlink($new_fn); } if ($type == 1) { imagetruecolortopalette($new_img, false, 255); $r = imagegif($new_img, $new_fn); } elseif ($type == 2) { imageinterlace($new_img, $interlace); $r = imagejpeg($new_img, $new_fn, $q); } elseif ($type == 3) { $r = imagepng($new_img, $new_fn, $q); } if (!$r) { trigger_error('error_creating_new_image', array('{file}' => $old_fn)); } return $r; } else { trigger_error('error_creating_new_image', array('{file}' => $old_fn)); return false; } }
public static function run($res, $rotation = NULL, $background = NULL) { // replace transparent background with another color (imagefill or via imagecolortransparent) $color = self::html2rgb($background); $stubColor = imagecolorallocate($res, $color[0], $color[1], $color[2]); imagefill($res, 0, 0, $stubColor); $imgcolors = imagecolorstotal($res); // rotate the image anticlockwise $resRotated = imagerotate($res, $rotation, $stubColor); imagedestroy($res); // replace color with transparent imagetruecolortopalette($resRotated, false, $imgcolors); imagecolortransparent($resRotated, imagecolorat($resRotated, 0, 0)); return $resRotated; }
/** * Applies the filter to the resource * * @param ImageResource $aResource */ public function applyFilter(ImageResource $aResource) { $dest = $aResource->getResource(); if (imageistruecolor($dest)) { imagetruecolortopalette($dest, false, 256); } foreach ($this->search as $search) { $searchRgb = new Color($search); $index = imagecolorclosest($aResource->getResource(), $searchRgb->getRed(), $searchRgb->getGreen(), $searchRgb->getBlue()); // get White COlor imagecolorset($aResource->getResource(), $index, $this->replace->getRed(), $this->replace->getGreen(), $this->replace->getBlue()); // SET NEW COLOR } $aResource->setResource($dest); }
function keepTransparent($des) { $image = $this->image; if ($image->getFormat() == 'png') { imagealphablending($des, false); imagesavealpha($des, true); } elseif ($image->getFormat() == 'gif') { $gdimage =& $image->getImage(); $colorTransparent = imagecolortransparent($gdimage); imagepalettecopy($gdimage, $des); if ($colorTransparent > 0) { imagefill($des, 0, 0, $colorTransparent); imagecolortransparent($des, $colorTransparent); } imagetruecolortopalette($des, true, 256); } }
public static function png($frame, $filename = false, $pixelPerPoint = 4, $outerFrame = 4, $saveandprint = FALSE) { $image = self::image($frame, $pixelPerPoint, $outerFrame); imagetruecolortopalette($image, false, 255); if ($filename === false) { Header("Content-type: image/png"); ImagePng($image); } else { if ($saveandprint === TRUE) { ImagePng($image, $filename); header("Content-type: image/png"); ImagePng($image); } else { ImagePng($image, $filename); } } ImageDestroy($image); }
function image_picture() { // draws large image // input data: [0] => image_id $function_arguments = func_get_args(); global $image_data; data_fetch("image", "SELECT * FROM `subrosian_tattoos` WHERE `id`=" . $function_arguments[0]); $file_src1 = $_SERVER['DOCUMENT_ROOT'] . "/images/main/Members/generic.pink.large.gif"; $file_src2 = $_SERVER['DOCUMENT_ROOT'] . "/images/main/Tattoos/tattoo" . $image_data['id'] . "." . $image_data['filetype']; $image_fileinfo1 = getimagesize($file_src1); $image_fileinfo2 = getimagesize($file_src2); $image_src1 = imagecreatefromgif($file_src1); switch ($image_fileinfo2[2]) { case 1: $image_src2 = imagecreatefromgif($file_src2); break; case 2: $image_src2 = imagecreatefromjpeg($file_src2); break; case 3: $image_src2 = imagecreatefrompng($file_src2); break; } $image_dest = imagecreatetruecolor($image_fileinfo1[0], $image_fileinfo1[1]); $image_width3 = $image_data['scale'] / 100 * ($image_fileinfo2[0] * abs(cos(M_PI * $image_data['rotation'] / 180)) + $image_fileinfo2[1] * abs(sin(M_PI * $image_data['rotation'] / 180))); $image_height3 = $image_data['scale'] / 100 * ($image_fileinfo2[0] * abs(sin(M_PI * $image_data['rotation'] / 180)) + $image_fileinfo2[1] * abs(cos(M_PI * $image_data['rotation'] / 180))); $image_src3 = imagecreatetruecolor($image_width3, $image_height3); $image_width2 = $image_width3 * 100 / $image_data['scale']; $image_height2 = $image_height3 * 100 / $image_data['scale']; $image_bg_fill = imagecolorat($image_src2, 5, 5); $image_src2 = imagerotate($image_src2, $image_data['rotation'], $image_bg_fill); imagecopy($image_dest, $image_src1, 0, 0, 0, 0, $image_fileinfo1[0], $image_fileinfo1[1]); imagecopyresized($image_src3, $image_src2, 0, 0, 0, 0, $image_width3, $image_height3, $image_width2, $image_height2); imagetruecolortopalette($image_src3, 0, 2); imagecolortransparent($image_src3, imagecolorat($image_src3, 0, 0)); imagecopymerge($image_dest, $image_src3, $image_data['x'], $image_data['y'], 0, 0, $image_width3, $image_height3, $image_data['strength']); imagefilter($image_dest, IMG_FILTER_SMOOTH, 80); imagetruecolortopalette($image_dest, 0, 128); return $image_dest; imagedestroy($image_src3); imagedestroy($image_src2); imagedestroy($image_src1); imagedestroy($image_dest); }
public function execute() { $this->params['filter_r'] = (int) $this->params['filter_r']; if ($this->params['filter_r'] < 0) { return; } $this->params['filter_g'] = (int) $this->params['filter_g']; if ($this->params['filter_g'] < 0) { return; } $this->params['filter_b'] = (int) $this->params['filter_b']; if ($this->params['filter_b'] < 0) { return; } $this->media->asImage(); $img = $this->media->getImage(); if (!($t = imagecolorstotal($img))) { $t = 256; imagetruecolortopalette($img, true, $t); } $imagex = imagesx($img); $imagey = imagesy($img); $gdimage = $this->media->getImage(); $w = $this->media->getWidth(); $h = $this->media->getHeight(); $src_x = ceil($w); $src_y = ceil($h); $dst_x = $src_x; $dst_y = $src_y; $dst_im = imagecreatetruecolor($dst_x, $dst_y); imagecopyresampled($dst_im, $gdimage, 0, 0, 0, 0, $dst_x, $dst_y, $src_x, $src_y); for ($y = 0; $y < $src_y; ++$y) { for ($x = 0; $x < $src_x; ++$x) { $rgb = imagecolorat($dst_im, $x, $y); $TabColors = imagecolorsforindex($dst_im, $rgb); $color_r = floor($TabColors['red'] * $this->params['filter_r'] / 255); $color_g = floor($TabColors['green'] * $this->params['filter_g'] / 255); $color_b = floor($TabColors['blue'] * $this->params['filter_b'] / 255); $newcol = imagecolorallocate($dst_im, $color_r, $color_g, $color_b); imagesetpixel($dst_im, $x, $y, $newcol); } } $this->media->setImage($dst_im); }
public static function image_resize($file_in, $file_out, $width, $height) { if (!file_exists($file_in)) { return FALSE; } $imagesize = getimagesize($file_in); if (!$width && !$height || !$imagesize[0] || !$imagesize[1]) { return FALSE; } if ($imagesize[0] == $width && $imagesize[1] == $height) { return copy($file_in, $file_out); } switch ($imagesize[2]) { case 1: $img = imagecreatefromgif($file_in); break; case 2: $img = imagecreatefromjpeg($file_in); break; case 3: $img = imagecreatefrompng($file_in); break; default: return FALSE; break; } if (!$img) { return FALSE; } $img2 = imagecreatetruecolor($width, $height); imagecopyresampled($img2, $img, 0, 0, 0, 0, $width, $height, $imagesize[0], $imagesize[1]); if ($imagesize[2] == 2) { return imagejpeg($img2, $file_out); } else { if ($imagesize[2] == 1 && function_exists("imagegif")) { imagetruecolortopalette($img2, FALSE, 256); return imagegif($img2, $file_out); } else { return imagepng($img2, $file_out); } } }
function save() { if (isset($this->thumbnailDataString)) { $hash = md5($this->thumbnailDataString); $this->thumbnailFileName = $hash; $original = imagecreatefromstring(base64_decode($this->thumbnailDataString)); $originalWidth = imagesx($original); $originalHeight = imagesy($original); $thumbMid = imagecreatetruecolor(self::THUMBNAIL_MID_DIMENSIONS, self::THUMBNAIL_MID_DIMENSIONS); imagecopyresampled($thumbMid, $original, 0, 0, 0, 0, self::THUMBNAIL_MID_DIMENSIONS, self::THUMBNAIL_MID_DIMENSIONS, $originalWidth, $originalHeight); $thumbSmall = imagecreatetruecolor(self::THUMBNAIL_SMALL_DIMENSIONS, self::THUMBNAIL_SMALL_DIMENSIONS); imagecopyresampled($thumbSmall, $original, 0, 0, 0, 0, self::THUMBNAIL_SMALL_DIMENSIONS, self::THUMBNAIL_SMALL_DIMENSIONS, $originalWidth, $originalHeight); imagetruecolortopalette($original, false, 8); imagetruecolortopalette($thumbMid, false, 8); imagetruecolortopalette($thumbSmall, false, 8); imagegif($original, self::FILE_PATH . $this->thumbnailFileName . '.gif'); imagegif($thumbMid, self::FILE_PATH . $this->thumbnailFileName . '_m.gif'); imagegif($thumbSmall, self::FILE_PATH . $this->thumbnailFileName . '_s.gif'); } }
function analyzeImageColors($im, $xCount = 3, $yCount = 3) { //get dimensions for image $imWidth = imagesx($im); $imHeight = imagesy($im); //find out the dimensions of the blocks we're going to make $blockWidth = round($imWidth / $xCount); $blockHeight = round($imHeight / $yCount); //now get the image colors... for ($x = 0; $x < $xCount; $x++) { //cycle through the x-axis for ($y = 0; $y < $yCount; $y++) { //cycle through the y-axis //this is the start x and y points to make the block from $blockStartX = $x * $blockWidth; $blockStartY = $y * $blockHeight; //create the image we'll use for the block $block = imagecreatetruecolor(1, 1); //We'll put the section of the image we want to get a color for into the block imagecopyresampled($block, $im, 0, 0, $blockStartX, $blockStartY, 1, 1, $blockWidth, $blockHeight); //the palette is where I'll get my color from for this block imagetruecolortopalette($block, true, 1); //I create a variable called eyeDropper to get the color information $eyeDropper = imagecolorat($block, 0, 0); $palette = imagecolorsforindex($block, $eyeDropper); $colorArray[$x][$y]['r'] = $palette['red']; $colorArray[$x][$y]['g'] = $palette['green']; $colorArray[$x][$y]['b'] = $palette['blue']; //get the rgb value too $hex = sprintf("%02X%02X%02X", $colorArray[$x][$y]['r'], $colorArray[$x][$y]['g'], $colorArray[$x][$y]['b']); $colorArray[$x][$y]['rgbHex'] = $hex; //destroy the block imagedestroy($block); } } //destroy the source image imagedestroy($im); return $colorArray; }