static function load($filename) { $info = getimagesize($filename); list($width, $height) = $info; if (!$width || !$height) { return null; } $image = null; switch ($info['mime']) { case 'image/gif': $image = imagecreatefromgif($filename); break; case 'image/jpeg': $image = imagecreatefromjpeg($filename); break; case 'image/png': $image = imagecreatetruecolor($width, $height); $white = imagecolorallocate($image, 255, 255, 255); imagefill($image, 0, 0, $white); $png = imagecreatefrompng($filename); imagealphablending($png, true); imagesavealpha($png, true); imagecopy($image, $png, 0, 0, 0, 0, $width, $height); imagedestroy($png); break; } if ($image) { return new image($image, $width, $height); } else { return null; } }
public function getAvatar($string, $widthHeight = 12, $theme = 'default') { $widthHeight = max($widthHeight, 12); $md5 = md5($string); $fileName = _TMP_DIR_ . '/' . $md5 . '.png'; if ($this->tmpFileExists($fileName)) { return $fileName; } // Create seed. $seed = intval(substr($md5, 0, 6), 16); mt_srand($seed); $body = array('legs' => mt_rand(0, count($this->availableParts[$theme]['legs']) - 1), 'hair' => mt_rand(0, count($this->availableParts[$theme]['hair']) - 1), 'arms' => mt_rand(0, count($this->availableParts[$theme]['arms']) - 1), 'body' => mt_rand(0, count($this->availableParts[$theme]['body']) - 1), 'eyes' => mt_rand(0, count($this->availableParts[$theme]['eyes']) - 1), 'mouth' => mt_rand(0, count($this->availableParts[$theme]['mouth']) - 1)); // Avatar random parts. $parts = array('legs' => $this->availableParts[$theme]['legs'][$body['legs']], 'hair' => $this->availableParts[$theme]['hair'][$body['hair']], 'arms' => $this->availableParts[$theme]['arms'][$body['arms']], 'body' => $this->availableParts[$theme]['body'][$body['body']], 'eyes' => $this->availableParts[$theme]['eyes'][$body['eyes']], 'mouth' => $this->availableParts[$theme]['mouth'][$body['mouth']]); $avatar = imagecreate($widthHeight, $widthHeight); imagesavealpha($avatar, true); imagealphablending($avatar, false); $background = imagecolorallocate($avatar, 0, 0, 0); $line_colour = imagecolorallocate($avatar, mt_rand(0, 200) + 55, mt_rand(0, 200) + 55, mt_rand(0, 200) + 55); imagecolortransparent($avatar, $background); imagefilledrectangle($avatar, 0, 0, $widthHeight, $widthHeight, $background); // Fill avatar with random parts. foreach ($parts as &$part) { $this->drawPart($part, $avatar, $widthHeight, $line_colour, $background); } imagepng($avatar, $fileName); imagecolordeallocate($avatar, $line_colour); imagecolordeallocate($avatar, $background); imagedestroy($avatar); return $fileName; }
/** * @param Frame $frame * @param $index * @return resource */ protected function render(Frame $frame, $index) { if ($index == 0) { $screenSize = $this->decoder->getScreenSize(); $im = imagecreatetruecolor($screenSize->getWidth(), $screenSize->getHeight()); imagealphablending($im, false); imagesavealpha($im, true); $transColor = imagecolortransparent($im, imagecolorallocatealpha($im, 255, 255, 255, 127)); imagefill($im, 0, 0, $transColor); $this->frameCurrent = $im; $this->framePrevious = $frame; $this->copyFrameToBuffer($frame); return $this->frameCurrent; } imagepalettetotruecolor($this->frameCurrent); $disposalMethod = $this->framePrevious->getDisposalMethod(); if ($disposalMethod === 0 || $disposalMethod === 1) { $this->copyFrameToBuffer($frame); } elseif ($disposalMethod === 2) { $this->restoreToBackground($this->framePrevious, imagecolortransparent($this->frameCurrent)); $this->copyFrameToBuffer($frame); } else { throw new \RuntimeException("Disposal method {$disposalMethod} is not implemented."); } $this->framePrevious = $frame; return $this->frameCurrent; }
public function save() { $maxHeight = 0; $width = 0; foreach ($this->_segmentsArray as $segment) { $maxHeight = max($maxHeight, $segment->height); $width += $segment->width; } // create our canvas $img = imagecreatetruecolor($width, $maxHeight); $background = imagecolorallocatealpha($img, 255, 255, 255, 127); imagefill($img, 0, 0, $background); imagealphablending($img, false); imagesavealpha($img, true); // start placing our images on a single x axis $xPos = 0; foreach ($this->_segmentsArray as $segment) { $tmp = imagecreatefromjpeg($segment->pathToImage); imagecopy($img, $tmp, $xPos, 0, 0, 0, $segment->width, $segment->height); $xPos += $segment->width; imagedestroy($tmp); } // create our final output image. imagepng($img, $this->_saveToPath); }
function embroidery2image($embroidery, $scale_post = 1, $scale_pre = false) { // Create image $im = imagecreatetruecolor(ceil($embroidery->imageWidth * $scale_post), ceil($embroidery->imageHeight * $scale_post)); imagesavealpha($im, true); imagealphablending($im, false); $color = imagecolorallocatealpha($im, 255, 255, 255, 127); imagefill($im, 0, 0, $color); // Draw stitches foreach ($embroidery->blocks as $block) { $color = imagecolorallocate($im, $block->color->r, $block->color->g, $block->color->b); $x = false; foreach ($block->stitches as $stitch) { if ($x !== false) { imageline($im, ($x - $embroidery->min->x) * $scale_post, ($y - $embroidery->min->y) * $scale_post, ($stitch->x - $embroidery->min->x) * $scale_post, ($stitch->y - $embroidery->min->y) * $scale_post, $color); } $x = $stitch->x; $y = $stitch->y; } } // Scale finished image if ($scale_pre) { $im2 = imagecreatetruecolor($embroidery->imageWidth * $scale_post * $scale_pre, $embroidery->imageHeight * $scale_post * $scale_pre); imagesavealpha($im2, true); imagealphablending($im2, false); imagecopyresized($im2, $im, 0, 0, 0, 0, $embroidery->imageWidth * $scale_post * $scale_pre, $embroidery->imageHeight * $scale_post * $scale_pre, $embroidery->imageWidth * $scale_post, $embroidery->imageHeight * $scale_post); imagedestroy($im); $im = $im2; } return $im; }
/** * Background fill an image using the provided color * * @param int $width The desired width of the new image * @param int $height The desired height of the new image * @param Array the desired pad colors in RGB format, array should be array( 'top' => '', 'right' => '', 'bottom' => '', 'left' => '' ); */ private function fill_color(array $colors) { $current_size = $this->editor->get_size(); $size = array('width' => $this->args['width'], 'height' => $this->args['height']); $offsetLeft = ($size['width'] - $current_size['width']) / 2; $offsetTop = ($size['height'] - $current_size['height']) / 2; $new_image = imagecreatetruecolor($size['width'], $size['height']); // This is needed to support alpha imagesavealpha($new_image, true); imagealphablending($new_image, false); // Check if we are padding vertically or horizontally if ($current_size['width'] != $size['width']) { $colorToPaint = imagecolorallocatealpha($new_image, substr($colors['left'], 0, 3), substr($colors['left'], 3, 3), substr($colors['left'], 6, 3), substr($colors['left'], 9, 3)); // Fill left color imagefilledrectangle($new_image, 0, 0, $offsetLeft + 5, $size['height'], $colorToPaint); $colorToPaint = imagecolorallocatealpha($new_image, substr($colors['right'], 0, 3), substr($colors['right'], 3, 3), substr($colors['right'], 6, 3), substr($colors['left'], 9, 3)); // Fill right color imagefilledrectangle($new_image, $offsetLeft + $current_size['width'] - 5, 0, $size['width'], $size['height'], $colorToPaint); } elseif ($current_size['height'] != $size['height']) { $colorToPaint = imagecolorallocatealpha($new_image, substr($colors['top'], 0, 3), substr($colors['top'], 3, 3), substr($colors['top'], 6, 3), substr($colors['left'], 9, 3)); // Fill top color imagefilledrectangle($new_image, 0, 0, $size['width'], $offsetTop + 5, $colorToPaint); $colorToPaint = imagecolorallocatealpha($new_image, substr($colors['bottom'], 0, 3), substr($colors['bottom'], 3, 3), substr($colors['bottom'], 6, 3), substr($colors['left'], 9, 3)); // Fill bottom color imagefilledrectangle($new_image, 0, $offsetTop - 5 + $current_size['height'], $size['width'], $size['height'], $colorToPaint); } imagecopy($new_image, $this->editor->get_image(), $offsetLeft, $offsetTop, 0, 0, $current_size['width'], $current_size['height']); $this->editor->update_image($new_image); $this->editor->update_size(); }
private static function image($frame, $pixelPerPoint = 4, $outerFrame = 4) { $h = count($frame); $w = strlen($frame[0]); $imgW = $w + 2 * $outerFrame; $imgH = $h + 2 * $outerFrame; $base_image = imagecreatetruecolor($imgW, $imgH); $col[0] = ImageColorAllocate($base_image, 255, 0, 255); $col[1] = ImageColorAllocate($base_image, 0, 0, 0); imagecolortransparent($base_image, $col[0]); imagealphablending($base_image, true); imagesavealpha($base_image, true); // imagefill($base_image, 0, 0, $col[0]); imagefill($base_image, 0, 0, 0x7fff0000); for ($y = 0; $y < $h; $y++) { for ($x = 0; $x < $w; $x++) { if ($frame[$y][$x] == '1') { ImageSetPixel($base_image, $x + $outerFrame, $y + $outerFrame, $col[1]); } } } $target_image = ImageCreate($imgW * $pixelPerPoint, $imgH * $pixelPerPoint); ImageCopyResized($target_image, $base_image, 0, 0, 0, 0, $imgW * $pixelPerPoint, $imgH * $pixelPerPoint, $imgW, $imgH); ImageDestroy($base_image); return $target_image; }
/** * Wrapper function for 'imagecopyresampled' * * @param Image $image * @param integer $dst_x * @param integer $dst_y * @param integer $src_x * @param integer $src_y * @param integer $dst_w * @param integer $dst_h * @param integer $src_w * @param integer $src_h * @return boolean */ protected function modify($image, $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h) { foreach ($image as $frame) { // create new image $modified = imagecreatetruecolor($dst_w, $dst_h); // get current image $resource = $frame->getCore(); // preserve transparency $transIndex = imagecolortransparent($resource); if ($transIndex != -1) { $rgba = imagecolorsforindex($modified, $transIndex); $transColor = imagecolorallocatealpha($modified, $rgba['red'], $rgba['green'], $rgba['blue'], 127); imagefill($modified, 0, 0, $transColor); imagecolortransparent($modified, $transColor); } else { imagealphablending($modified, false); imagesavealpha($modified, true); } // copy content from resource imagecopyresampled($modified, $resource, $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h); // free memory of old core imagedestroy($resource); // set new content as recource $frame->setCore($modified); } return true; }
public static function run($res, $width = NULL, $height = NULL) { $dst_w = Image::width($res); $dst_h = Image::height($res); if (!empty($width) && !empty($height)) { $dst_w = $width; $dst_h = $height; } elseif (empty($height)) { $ratio = $dst_h / $dst_w; $dst_w = $width; $dst_h = round($dst_w * $ratio); } elseif (empty($width)) { $ratio = $dst_w / $dst_h; $dst_h = $height; $dst_w = round($dst_h * $ratio); } $dst = imagecreatetruecolor($dst_w, $dst_h); /* making the new image transparent */ $background = imagecolorallocate($dst, 0, 0, 0); ImageColorTransparent($dst, $background); // make the new temp image all transparent imagealphablending($dst, false); imagesavealpha($dst, true); imageAntiAlias($dst, true); self::__fill($dst); imagecopyresampled($dst, $res, 0, 0, 0, 0, $dst_w, $dst_h, Image::width($res), Image::height($res)); @imagedestroy($res); return $dst; }
public function getImage($data) { $pngData = $this->encode($data); if ($pngData == null) { return null; } $h = count($pngData); $w = strlen($pngData[0]); $imgW = $w + 2 * $this->outerFrame; $imgH = $h + 2 * $this->outerFrame; $qrcode_image = imagecreatetruecolor($imgW, $imgH); imagealphablending($qrcode_image, false); $qrBackColor = imagecolorallocatealpha($qrcode_image, 255, 255, 255, 127); imagefill($qrcode_image, 0, 0, $qrBackColor); imagesavealpha($qrcode_image, true); if (!empty($this->fgcolor) && (substr($this->fgcolor, 0, 1) == '#' && strlen(trim($this->fgcolor)) == 7 || strlen(trim($this->fgcolor)) == 6)) { $rgb = str_split(ltrim($this->fgcolor, '#'), 2); $qrColor = imagecolorallocatealpha($qrcode_image, hexdec($rgb[0]), hexdec($rgb[1]), hexdec($rgb[2]), 0); } else { $qrColor = imagecolorallocatealpha($qrcode_image, 0, 0, 0, 0); } for ($y = 0; $y < $h; $y++) { for ($x = 0; $x < $w; $x++) { if ($pngData[$y][$x] == '1') { imagesetpixel($qrcode_image, $x + $this->outerFrame, $y + $this->outerFrame, $qrColor); } } } return $qrcode_image; }
function createImage($name, $filename, $new_w, $new_h) { $system2 = explode('.', strtolower(basename($filename))); $system2[1] = $system2[1]; $src_img = imagecreatefromstring(readFileData($name)); $old_w = imageSX($src_img); $old_h = imageSY($src_img); $thumb_w = $new_w; $thumb_h = $new_h; if ($new_w > $old_w) { $thumb_w = $old_w; $thumb_h = $thumb_w / $old_w * $old_h; } else { $thumb_w = $new_w; $thumb_h = $thumb_w / $old_w * $old_h; } if ($thumb_h > $new_h) { $thumb_h = $new_h; $thumb_w = $thumb_h / $old_h * $old_w; } $dst_img = ImageCreateTrueColor($thumb_w, $thumb_h); imagealphablending($dst_img, false); imagesavealpha($dst_img, true); $transparent = imagecolorallocatealpha($dst_img, 255, 255, 255, 127); imagefilledrectangle($dst_img, 0, 0, $thumb_w, $thumb_h, $transparent); imagecopyresampled($dst_img, $src_img, 0, 0, 0, 0, $thumb_w, $thumb_h, $old_w, $old_h); if (preg_match("/png/", $system2[1])) { imagepng($dst_img, $filename); } else { imagejpeg($dst_img, $filename, 90); } imagedestroy($dst_img); imagedestroy($src_img); }
/** * @param int $width Target width * @param int $height Target height * @param bool $toFit If true, image fill fit to given dimensions, if false, it will cover them * @param bool $force If true, image will be resized even if target dimensions are larger than original */ protected function scale($width, $height, $toFit, $force) { if (null === $this->_image) { return; } $rawWidth = $this->_getWidth(); $rawHeight = $this->_getHeight(); $widthOver = $rawWidth / $width; $heightOver = $rawHeight / $height; if ($toFit) { $scalingFactor = max($widthOver, $heightOver); } else { $scalingFactor = min($widthOver, $heightOver); } if ($scalingFactor > 1 || $force) { $destWidth = $rawWidth / $scalingFactor; $destHeight = $rawHeight / $scalingFactor; $destImage = imagecreatetruecolor($destWidth, $destHeight); imagealphablending($destImage, false); imagesavealpha($destImage, true); $transparent = imagecolorallocatealpha($destImage, 255, 255, 255, 127); imagefill($destImage, 0, 0, $transparent); imagecopyresampled($destImage, $this->_image, 0, 0, 0, 0, $destWidth, $destHeight, $rawWidth, $rawHeight); $this->_image = $destImage; } }
function resize($src,$dst,$dstw,$dsth,$scala,$percorsosave) { $src = $percorsosave.$src; $dst = $percorsosave.$dst; list($width, $height, $type, $attr) = getimagesize($src); switch($type){ case 1:$im = imagecreatefromgif($src);break; case 2:$im = imagecreatefromjpeg($src);break; case 3:$im = imagecreatefrompng($src);break; case 8:$im = imagecreatefromwbmp($src);break; default:break; } If ($dstw == "0" && $dsth == "0") { $dstw = $width; $dsth = $height; } switch($scala){ //scala in base alla lunghezza case 1: $dsth=($height*$dstw)/$width; break; //scala in base all'altezza case 2: $dstw=($width*$dsth)/$height; break; default:break; }; $tim = imagecreatetruecolor($dstw,$dsth); imagesavealpha($tim,true); imagealphablending($tim,false); imagecopyresampled($tim,$im,0,0,0,0,$dstw,$dsth,$width,$height); ImageJPEG($tim,$dst,90); imagedestroy($tim); }
public function prepare() { if (!isset($this->img['src'])) { $this->gifsupport = function_exists('imagegif'); // if mimetype detected and in imagemap -> change format if (class_exists("finfo") && ($finfo = new finfo(FILEINFO_MIME_TYPE))) { if ($ftype = @$finfo->file($this->img['filepath'])) { if (array_key_exists($ftype, $this->image_mimetype_map)) { $this->img['format'] = $this->image_mimetype_map[$ftype]; } } } // ----- detect image format if ($this->img['format'] == 'jpg' || $this->img['format'] == 'jpeg') { $this->img['format'] = 'jpeg'; $this->img['src'] = @imagecreatefromjpeg($this->img['filepath']); } elseif ($this->img['format'] == 'png') { $this->img['src'] = @imagecreatefrompng($this->img['filepath']); imagealphablending($this->img['src'], false); imagesavealpha($this->img['src'], true); } elseif ($this->img['format'] == 'gif') { if ($this->gifsupport) { $this->img['src'] = @imagecreatefromgif($this->img['filepath']); } } elseif ($this->img['format'] == 'wbmp') { $this->img['src'] = @imagecreatefromwbmp($this->img['filepath']); } if (isset($this->img['src'])) { $this->refreshDimensions(); } } }
public function addAttachments($con) { $decoded = null; if (strpos($this->link, 'http://') === false && strpos($this->link, 'https://') === false) { $decoded = base64_decode(preg_replace('#^data:image/\\w+;base64,#i', '', $this->link)); } if ($decoded) { $name = $this->message_id . "-" . md5($decoded); $filename = __DIR__ . '/../../images/' . $name . '.png'; if (!file_exists($filename)) { $img = imagecreatefromstring($decoded); imagealphablending($img, false); imagesavealpha($img, true); imagepng($img, $filename); } $this->link = $name . '.png'; } $query = "INSERT INTO attachments VALUES(" . $this->message_id . ", '" . $this->link . "', '" . $this->title . "')"; if ($con) { $res = $con->exec($query); } else { $res = db::query($query); } $arr = array('id' => $this->message_id, 'link' => $this->link, 'title' => $this->title); return $arr; }
/** * Create thumbnail image by php using the GD Library * * @since 1.0.0 */ public static function createThumb($source_image, $destination_image_url, $width, $height, $quality, $crop = null, $prefix = null, $checksize = null) { //Set image ratio list($w, $h) = getimagesize($source_image); // resize if ($crop === true) { if ($checksize && ($w < $width or $h < $height)) { $width = $w + 1; $height = $h + 1; $x = 0; } else { $ratio = max($width / $w, $height / $h); $h = $height / $ratio; $x = ($w - $width / $ratio) / 2; $w = $width / $ratio; } } else { if ($checksize && ($w < $width or $h < $height)) { $width = $w; $height = $h; $x = 0; } else { $ratio = min($width / $w, $height / $h); $width = $w * $ratio; $height = $h * $ratio; $x = 0; } } if (preg_match("/.jpg/i", "{$source_image}") or preg_match("/.jpeg/i", "{$source_image}")) { //JPEG type thumbnail $destImage = imagecreatetruecolor($width, $height); $sourceImage = imagecreatefromjpeg($source_image); imagecopyresampled($destImage, $sourceImage, 0, 0, $x, 0, $width, $height, $w, $h); imagejpeg($destImage, $destination_image_url, $quality); imagedestroy($destImage); } elseif (preg_match("/.png/i", "{$source_image}")) { //PNG type thumbnail $destImage = imagecreatetruecolor($width, $height); $sourceImage = imagecreatefrompng($source_image); imagealphablending($destImage, false); imagecopyresampled($destImage, $sourceImage, 0, 0, $x, 0, $width, $height, $w, $h); imagesavealpha($destImage, true); imagepng($destImage, $destination_image_url); } elseif (preg_match("/.gif/i", "{$source_image}")) { //GIF type thumbnail $destImage = imagecreatetruecolor($width, $height); $sourceImage = imagecreatefromgif($source_image); $bgc = imagecolorallocate($destImage, 255, 255, 255); imagefilledrectangle($destImage, 0, 0, $width, $height, $bgc); imagecopyresampled($destImage, $sourceImage, 0, 0, $x, 0, $width, $height, $w, $h); if (function_exists('imagegif')) { // Pour GIF header('Content-Type: image/gif'); imagegif($destImage, $destination_image_url, $quality); } imagedestroy($destImage); } else { echo 'unable to load image source'; } }
public function action_thumb() { if (!preg_match('/^image\\/.*$/i', $this->attachment['mime'])) { $ext = File::ext_by_mime($this->attachment['mime']); if (file_exists(DOCROOT . 'img/icons/' . $ext . '-icon-128x128.png')) { $this->redirect('/img/icons/' . $ext . '-icon-128x128.png'); } else { $this->redirect('http://stdicon.com/' . $this->attachment['mime'] . '?size=96&default=http://stdicon.com/text'); } } if (!file_exists(DOCROOT . 'storage/' . $this->attachment['id'] . '.thumb')) { if (!file_exists(DOCROOT . 'storage/' . $this->attachment['id'])) { $this->redirect('http://stdicon.com/' . $this->attachment['mime'] . '?size=96&default=http://stdicon.com/text'); } $data = file_get_contents(DOCROOT . 'storage/' . $this->attachment['id']); $image = imagecreatefromstring($data); $x = imagesx($image); $y = imagesy($image); $size = max($x, $y); $x = round($x / $size * 96); $y = round($y / $size * 96); $thumb = imagecreatetruecolor($x, $y); imagealphablending($thumb, false); imagesavealpha($thumb, true); imagecopyresampled($thumb, $image, 0, 0, 0, 0, $x, $y, imagesx($image), imagesy($image)); imagepng($thumb, DOCROOT . 'storage/' . $this->attachment['id'] . '.thumb', 9); } header('Content-type: image/png'); header('Content-disposition: filename="thumbnail.png"'); header('Content-length: ' . filesize(DOCROOT . 'storage/' . $this->attachment['id'] . '.thumb')); readfile(DOCROOT . 'storage/' . $this->attachment['id'] . '.thumb'); die; }
public function Resize($image, $newWidth, $targetName) { if (!file_exists(PUBLIC_ROOT . $image)) { $image = '/assets/images/not-found.gif'; } $imgInfo = getimagesize(PUBLIC_ROOT . $image); $oldWidth = $imgInfo[0]; $oldHeight = $imgInfo[1]; $changeRatio = $oldWidth / $newWidth; $newHeight = round($oldHeight / $changeRatio); $newImage = imagecreatetruecolor($newWidth, $newHeight); $source = $this->load(PUBLIC_ROOT . $image); if ($this->imageType == IMAGETYPE_PNG) { imagealphablending($newImage, false); imagesavealpha($newImage, true); $transparent = imagecolorallocatealpha($newImage, 255, 255, 255, 127); imagefilledrectangle($newImage, 0, 0, $newWidth, $newHeight, $transparent); } imagecopyresampled($newImage, $this->image, 0, 0, 0, 0, $newWidth, $newHeight, $oldWidth, $oldHeight); header('Content-Type: image/jpeg'); imagejpeg($newImage, $targetName, 100); Debugger::debug($targetName, 'TARGET'); //$this->save($targetName); $this->image = $newImage; imagedestroy($newImage); }
function render_block($left_side, $top_side, $right_side) { global $size; $size = 2048; $x1 = (2 - sqrt(3)) * 0.25 * $size; $x2 = 0.5 * $size; $x3 = (2 + sqrt(3)) * 0.25 * $size; $y1 = 0; $y2 = 0.25 * $size; $y3 = 0.5 * $size; $y4 = 0.75 * $size; $y5 = $size; $first_poligon = array($x1, $y2, $x2, $y3, $x2, $y5, $x1, $y4); $second_poligon = array($x1, $y2, $x2, $y1, $x3, $y2, $x2, $y3); $third_poligon = array($x2, $y3, $x3, $y2, $x3, $y4, $x2, $y5); $im = imagecreatetruecolor($size, $size); // Transparentbackground imagealphablending($im, true); imagesavealpha($im, true); $trans = imagecolorallocatealpha($im, 0, 0, 0, 127); imagefill($im, 0, 0, $trans); imagetranslatedtexture($im, $first_poligon, imagelight(load_png($left_side), 96)); imagetranslatedtexture($im, $second_poligon, load_png($top_side)); imagetranslatedtexture($im, $third_poligon, imagelight(load_png($right_side), 64)); return $im; }
/** * @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(); }
namespace common; class upload { //从tmp中移动upload public static function zoom(&$file, &$maxWidth = 0, &$maxHeight = 0) { list($width, $height, $im, $func, $ext) = self::_init($file); if (!$im) { \yk\log::runlog('file upload error: im not found', 'upload'); return false; } if ($maxWidth > 0) { $p = max($width / $maxWidth, $height / $maxHeight); $dstwidth = intval($width / $p); $dstheight = intval($height / $p); } else { $dstwidth = $width; $dstheight = $height; } $maxWidth = $dstwidth; $maxHeight = $dstheight; $dstim = imagecreatetruecolor($dstwidth, $dstheight); imagealphablending($dstim, false); //关闭混杂模式,不可缺少, PHP文档中说明: (在非混色模式下,画笔颜色连同其 alpha 通道信息一起被拷贝,替换掉目标像素。混色模式在画调色板图像时不可用。) 而且是imagesavealpha方法起作用的前置步骤. imagesavealpha($dstim, true); //保存 PNG 图像时保存完整的 alpha 通道信息 $transparent = imagecolorallocatealpha($dstim, 255, 255, 255, 127); //取得一个透明的颜色, 透明度在 0-127 间 imagefill($dstim, 0, 0, $transparent); imagecopyresampled($dstim, $im, 0, 0, 0, 0, $dstwidth, $dstheight, $width, $height); $file = uniqid() . $ext;
function imagemergealpha($i) { //create a new image $s = imagecreatetruecolor(imagesx($i[0]), imagesy($i[1])); $back_color = imagecolorallocate($s, 0xa9, 0xb1, 0xd3); //merge all images imagealphablending($s, true); $z = $i; while ($d = each($z)) { imagecopy($s, $d[1], 0, 0, 0, 0, imagesx($d[1]), imagesy($d[1])); } //restore the transparency imagealphablending($s, false); $w = imagesx($s); $h = imagesy($s); for ($x = 0; $x < $w; $x++) { for ($y = 0; $y < $h; $y++) { $c = imagecolorat($s, $x, $y); $c = imagecolorsforindex($s, $c); $z = $i; $t = 0; while ($d = each($z)) { $ta = imagecolorat($d[1], $x, $y); $ta = imagecolorsforindex($d[1], $ta); $t += 127 - $ta['alpha']; } $t = $t > 127 ? 127 : $t; $t = 127 - $t; $c = imagecolorallocatealpha($s, $c['red'], $c['green'], $c['blue'], $t); imagesetpixel($s, $x, $y, $c); } } imagesavealpha($s, true); return $s; }
public function generate() { imagesavealpha($this->_owner->image, true); imagealphablending($this->_owner->image, true); $width = $this->_owner->getImageWidth(); $height = $this->_owner->getImageHeight(); $white = $this->_owner->imagecolorallocate($this->_text_color); $data = array(); $total_width = 0; for ($x = 0; $x < strlen($this->_text); $x++) { $data[$x]['text'] = $this->_text[$x]; $data[$x]['font'] = $this->_arr_ttf_font[rand(0, count($this->_arr_ttf_font) - 1)]; $data[$x]['size'] = rand($this->_text_size, $this->_text_size + $this->_text_size_random); $data[$x]['angle'] = $this->_text_angle_random / 2 - rand(0, $this->_text_angle_random); $captcha_dimensions = imagettfbbox($data[$x]['size'], $data[$x]['angle'], $data[$x]['font'], $data[$x]['text']); $data[$x]['width'] = abs($captcha_dimensions[2]) + $this->_text_spacing; $data[$x]['height'] = abs($captcha_dimensions[5]); $total_width += $data[$x]['width']; } $x_offset = ($width - $total_width) / 2; $x_pos = 0; $y_pos = 0; foreach ($data as $ld) { $y_pos = ($height + $ld['height']) / 2; imagettftext($this->_owner->image, $ld['size'], $ld['angle'], $x_offset + $x_pos, $y_pos, $white, $ld['font'], $ld['text']); $x_pos += $ld['width']; } return true; }
function merge($width, $height) { $array_variable = get_variable(); $bg = $array_variable[0]; $over = $array_variable[1]; $outputFile = $array_variable[2]; $result = $array_variable[3]; $path_to_save = $array_variable[4]; $n = $array_variable[5]; $tmp_img = $path_to_save . "/tmp_" . $n . ".png"; // $result_jpg_compressed = $path_to_save.'/'.$n.'.JPG'; // $base_image = imagecreatefrompng($bg); jpg2png($bg, $outputFile); $base_image = imagecreatefrompng($outputFile); $top_image = imagecreatefrompng($over); // $merged_image = $result; imagesavealpha($top_image, true); imagealphablending($top_image, true); imagecopy($base_image, $top_image, 0, 0, 0, 0, $width, $height); imagepng($base_image, $result); // rename to temp for compression rename($result, $path_to_save . "/tmp_" . $n . ".png"); // compress IMG $img = imagecreatefrompng($tmp_img); // imagejpeg($img,$result_jpg_compressed,75); imagejpeg($img, $result, 75); unlink($tmp_img); unlink($tmp_img); // if necessery !!!!!!!!!!!!!!!!!!! unlink($outputFile); unlink($over); }
/** +---------------------------------------------------------- * 显示服务器图像文件 * 支持URL方式 +---------------------------------------------------------- * @static * @access public +---------------------------------------------------------- * @param string $imgFile 图像文件名 * @param string $text 文字字符串 * @param string $width 图像宽度 * @param string $height 图像高度 +---------------------------------------------------------- * @return void +---------------------------------------------------------- */ static function showImg($imgFile, $text = '', $width = 80, $height = 30) { //获取图像文件信息 $info = Image::getImageInfo($imgFile); if ($info !== false) { $createFun = str_replace('/', 'createfrom', $info['mime']); $im = $createFun($imgFile); if ($im) { $ImageFun = str_replace('/', '', $info['mime']); if (!empty($text)) { $tc = imagecolorallocate($im, 0, 0, 0); imagestring($im, 3, 5, 5, $text, $tc); } if ($info['type'] == 'png' || $info['type'] == 'gif') { imagealphablending($im, false); //取消默认的混色模式 imagesavealpha($im, true); //设定保存完整的 alpha 通道信息 } Header("Content-type: " . $info['mime']); $ImageFun($im); @ImageDestroy($im); return; } } //获取或者创建图像文件失败则生成空白PNG图片 $im = imagecreatetruecolor($width, $height); $bgc = imagecolorallocate($im, 255, 255, 255); $tc = imagecolorallocate($im, 0, 0, 0); imagefilledrectangle($im, 0, 0, 150, 30, $bgc); imagestring($im, 4, 5, 5, "NO PIC", $tc); Image::output($im); return; }
public function resizeAndSaveImage($mimeType, $source_url, $destination_url) { // initial image buffer if ($mimeType == 'image/jpeg') { $image = imagecreatefromjpeg($source_url); } elseif ($mimeType == 'image/gif') { $image = imagecreatefromgif($source_url); } elseif ($mimeType == 'image/png') { $image = imagecreatefrompng($source_url); } // calc new image size keep aspect ratio $original_width = imagesx($image); $original_height = imagesy($image); // if current image is bigger then 500px, resize it to make it smaller $new_width = 500; if ($original_width > $new_width) { // calc new height by keep aspect ratio $new_height = $this->getResizeHeightByWidth($original_height, $original_width, $new_width); // resize with image buffer $new_image = imagecreatetruecolor($new_width, $new_height); imagecopyresampled($new_image, $image, 0, 0, 0, 0, $new_width, $new_height, $original_width, $original_height); // saving image as full-alpha imagealphablending($new_image, false); imagesavealpha($new_image, true); if ($mimeType == 'image/jpeg') { imagejpeg($new_image, $source_url); } elseif ($mimeType == 'image/gif') { imagegif($new_image, $source_url); } elseif ($mimeType == 'image/png') { imagepng($new_image, $source_url); } imagedestroy($new_image); } imagedestroy($image); }
public function resize($width = 0, $height = 0) { if (!$this->info['width'] || !$this->info['height']) { return; } $scale = min($width / $this->info['width'], $height / $this->info['height']); if ($scale == 1 && $this->info['mime'] != 'image/png') { return; } $new_width = (int) ($this->info['width'] * $scale); $new_height = (int) ($this->info['height'] * $scale); $xpos = (int) (($width - $new_width) / 2); $ypos = (int) (($height - $new_height) / 2); $image_old = $this->image; $this->image = imagecreatetruecolor($width, $height); if (isset($this->info['mime']) && $this->info['mime'] == 'image/png') { imagealphablending($this->image, false); imagesavealpha($this->image, true); $background = imagecolorallocatealpha($this->image, 255, 255, 255, 127); imagecolortransparent($this->image, $background); } else { $background = imagecolorallocate($this->image, 255, 255, 255); } imagefilledrectangle($this->image, 0, 0, $width, $height, $background); imagecopyresampled($this->image, $image_old, $xpos, $ypos, 0, 0, $new_width, $new_height, $this->info['width'], $this->info['height']); imagedestroy($image_old); $this->info['width'] = $width; $this->info['height'] = $height; }
/** * Add an image to the generator. * * This function adds a source image to the generator. It serves two main purposes: add a source image if one was * not supplied to the constructor and to add additional source images so that different images can be supplied for * different sized images in the resulting ICO file. For instance, a small source image can be used for the small * resolutions while a larger source image can be used for large resolutions. * * @param string $file Path to the source image file. * @param array $sizes Optional. An array of sizes (each size is an array with a width and height) that the source image should be rendered at in the generated ICO file. If sizes are not supplied, the size of the source image will be used. * @return boolean true on success and false on failure. */ public function add_image($file, $sizes = array()) { if (!$this->_has_requirements) { return false; } if (false === ($im = $this->_load_image_file($file))) { return false; } if (empty($sizes)) { $sizes = array(imagesx($im), imagesy($im)); } // If just a single size was passed, put it in array. if (!is_array($sizes[0])) { $sizes = array($sizes); } foreach ((array) $sizes as $size) { list($width, $height) = $size; $new_im = imagecreatetruecolor($width, $height); imagecolortransparent($new_im, imagecolorallocatealpha($new_im, 0, 0, 0, 127)); imagealphablending($new_im, false); imagesavealpha($new_im, true); $source_width = imagesx($im); $source_height = imagesy($im); if (false === imagecopyresampled($new_im, $im, 0, 0, 0, 0, $width, $height, $source_width, $source_height)) { continue; } $this->_add_image_data($new_im); } return true; }
/** * * @param string $playername Minecraft player name * @param resource $avatar the rendered avatar (for example player head) * * @param string $background Image Path or Standard Value * @return resource the generated banner */ public static function player($playername, $avatar = NULL, $background = NULL) { $canvas = MinecraftBanner::getBackgroundCanvas(self::PLAYER_WIDTH, self::PLAYER_HEIGHT, $background); $head_height = self::AVATAR_SIZE; $head_width = self::AVATAR_SIZE; $avater_x = self::PLAYER_PADDING; $avater_y = self::PLAYER_HEIGHT / 2 - self::AVATAR_SIZE / 2; if ($avatar == NULL) { $avatar = imagecreatefrompng(__DIR__ . "/img/head.png"); imagesavealpha($avatar, true); imagecopy($canvas, $avatar, $avater_x, $avater_y, 0, 0, $head_width, $head_height); } else { $head_width = imagesx($avatar); $head_height = imagesy($avatar); if ($head_width > self::AVATAR_SIZE) { $head_width = self::AVATAR_SIZE; } if ($head_height > self::AVATAR_SIZE) { $head_height = self::AVATAR_SIZE; } $center_x = $avater_x + self::AVATAR_SIZE / 2 - $head_width / 2; $center_y = $avater_y + self::AVATAR_SIZE / 2 - $head_height / 2; imagecopy($canvas, $avatar, $center_x, $center_y, 0, 0, $head_width, $head_height); } $box = imagettfbbox(self::TEXT_SIZE, 0, MinecraftBanner::FONT_FILE, $playername); $text_width = abs($box[4] - $box[0]); $text_color = imagecolorallocate($canvas, 255, 255, 255); $remaining = self::PLAYER_WIDTH - self::AVATAR_SIZE - $avater_x - self::PLAYER_PADDING; $text_posX = $avater_x + self::AVATAR_SIZE + $remaining / 2 - $text_width / 2; $text_posY = $avater_y + self::AVATAR_SIZE / 2 + self::TEXT_SIZE / 2; imagettftext($canvas, self::TEXT_SIZE, 0, $text_posX, $text_posY, $text_color, MinecraftBanner::FONT_FILE, $playername); return $canvas; }
private function _txt2img($string) { $fontfile = __DIR__ . '/fonts/FreeSans.ttf'; $font = (int) $this->params->get('fontsize', 14); $im = @imagecreatetruecolor(strlen($string) * $font / 1.7, $font); imagesavealpha($im, true); imagealphablending($im, false); $white = imagecolorallocatealpha($im, 255, 255, 255, 127); imagefill($im, 0, 0, $white); $rgb = $this->_hex2rgb($this->params->get('textcolor', '#000000')); //~ $alpha = (int)$this->params->get('alpha', '127'); $color = imagecolorallocate($im, $rgb[0], $rgb[1], $rgb[2]); imagettftext($im, $font, 0, 1, $font, $color, $fontfile, $string); ob_start(); imagepng($im); $image = ob_get_clean(); imagedestroy($im); ob_start(); ?> <img src="data:image/png;base64,<?php echo base64_encode($image); ?> " /> <?php return ob_get_clean(); }