/** * Generate a shirt image * Image::generateShirt('uploads/chuck.jpg', 'Member', 999, 200); * * @param string $url * @param integer $width * @param integer $height * @param boolean $crop * @return string */ public function generateShirt($url, $shirt_name = 'Member', $member_id = 0, $width = 200, $height = 200, $crop = true, $quality = 90) { if ($url) { // URL info $info = pathinfo($url); // The size if (!$height) { $height = $width; } // Font and text $text_offset = 64; $font_path = public_path() . '/fonts/trebuc.ttf'; $palette = new \Imagine\Image\Palette\RGB(); $font_color = $palette->color('#FFF'); $shirt_name = strtoupper(substr($shirt_name, 0, 12)); $shirt_name_length = strlen($shirt_name); $font_size = 16; if ($shirt_name_length < 6) { $font_size = 16; } else { if ($shirt_name_length > 5 && $shirt_name_length < 10) { $font_size = 11; } else { $font_size = 8; } } $font = $this->imagine->font($font_path, $font_size, $font_color); $font_width = $font->box($shirt_name, 0)->getWidth(); // Centering the text over the image in x axis $text_offset = (200 - $font_width) / 2; if ($text_offset < 64) { $text_offset = 64; } // Quality $quality = Config::get('image.quality', $quality); // Directories and file names $fileName = $info['basename']; $sourceDirPath = public_path() . '/' . $info['dirname']; $sourceFilePath = $sourceDirPath . '/' . $fileName; $targetDirName = $width . 'x' . $height . ($crop ? '_crop' : '') . '_' . $shirt_name . '_' . $member_id; $targetDirPath = $sourceDirPath . '/' . $targetDirName . '/'; $targetFilePath = $targetDirPath . $fileName; $targetUrl = asset($info['dirname'] . '/' . $targetDirName . '/' . $fileName); $fbTargetUrl = 'http://' . $_SERVER['SERVER_NAME'] . '/uploads/' . $targetDirName . '/' . $fileName; // Create directory if missing try { // Create dir if missing if (!File::isDirectory($targetDirPath) and $targetDirPath) { @File::makeDirectory($targetDirPath); } // Set the size $size = new \Imagine\Image\Box($width, $height); // Now the mode $mode = $crop ? \Imagine\Image\ImageInterface::THUMBNAIL_OUTBOUND : \Imagine\Image\ImageInterface::THUMBNAIL_INSET; if (!File::exists($targetFilePath) or File::lastModified($targetFilePath) < File::lastModified($sourceFilePath)) { $canvas = $this->imagine->open($sourceFilePath)->thumbnail($size, $mode); $canvas->draw()->text($shirt_name, $font, new \Imagine\Image\Point($text_offset, 54), 0); $canvas->save($targetFilePath, array('quality' => $quality)); } } catch (\Exception $e) { Log::error('[IMAGE SERVICE] Failed to resize image "' . $url . '" [' . $e->getMessage() . ']'); return false; } return $fbTargetUrl; } }
function generate_image($image, $typeobj, $id) { global $faker; $image->setTitle(getRealText(20))->setDescription(getRealText(250))->setChapo(getRealText(40))->setPostscriptum(getRealText(40))->setFile(sprintf("sample-image-%s.png", $id))->save(); $palette = new \Imagine\Image\Palette\RGB(); // Generate images $imagine = new Imagine\Gd\Imagine(); $image = $imagine->create(new Imagine\Image\Box(320, 240), $palette->color('#E9730F')); $white = $palette->color('#FFF'); $font = $imagine->font(__DIR__ . '/faker-assets/FreeSans.ttf', 14, $white); $tbox = $font->box("THELIA"); $image->draw()->text("THELIA", $font, new Imagine\Image\Point((320 - $tbox->getWidth()) / 2, 30)); $str = sprintf("%s sample image", ucfirst($typeobj)); $tbox = $font->box($str); $image->draw()->text($str, $font, new Imagine\Image\Point((320 - $tbox->getWidth()) / 2, 80)); $font = $imagine->font(__DIR__ . '/faker-assets/FreeSans.ttf', 18, $white); $str = sprintf("%s ID %d", strtoupper($typeobj), $id); $tbox = $font->box($str); $image->draw()->text($str, $font, new Imagine\Image\Point((320 - $tbox->getWidth()) / 2, 180)); $image->draw()->line(new Imagine\Image\Point(0, 0), new Imagine\Image\Point(319, 0), $white)->line(new Imagine\Image\Point(319, 0), new Imagine\Image\Point(319, 239), $white)->line(new Imagine\Image\Point(319, 239), new Imagine\Image\Point(0, 239), $white)->line(new Imagine\Image\Point(0, 239), new Imagine\Image\Point(0, 0), $white); $image_file = sprintf("%s/media/images/%s/sample-image-%s.png", THELIA_LOCAL_DIR, $typeobj, $id); if (!is_dir(dirname($image_file))) { mkdir(dirname($image_file), 0777, true); } $image->save($image_file); }
/** * {@inheritdoc} */ public static function font($file, $size, Imagine\Image\Palette\Color\ColorInterface $color) { return static::$instance->font($file, $size, $color); }