note 33054 deleted from ref.java by aidan

From: Date: Wed, 01 Sep 2004 00:08:49 +0000
Subject: note 33054 deleted from ref.java by aidan
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-75803@lists.php.net to get a copy of this message
Note Submitter: chris at mad-teaparty dot com ---- This snippet shows how to use Java's image-editing classes and methods in PHP. It creates a gradient-painted, shadowed text as a JPEG file. Note: - Unfortunately, it only works as a CGI. Tests failed with PHP compiled as an Apache Module on both Linux and Windows. - If you don't specify an absolute path for your image, it will be created in your PHP directory. - Running this script takes quite some time. (CGI, creating the JVM etc.) <?php // general settings $imgName = "php_java_image.jpg"; $message = "mad-teaparty.com"; $fontName = "Monospace"; $fontSize = 80; $startrgb = array(0, 0, 255); // color for gradient paint at the first point $endrgb = array(0, 0, 0); // color for gradient paint at the second point $shadowrgb = array(204, 204, 204); // color for the shadow $bgrgb = array(255, 255, 255); //color for the background // create instances of 'java.awt.Color' $endcol = new Java('java.awt.Color', $endrgb[0], $endrgb[1], $endrgb[2]); $shadowcol = new Java('java.awt.Color', $shadowrgb[0], $shadowrgb[1], $shadowrgb[2]); $startcol = new Java('java.awt.Color', $startrgb[0], $startrgb[1], $startrgb[2]); $bgcol = new Java('java.awt.Color', $bgrgb[0], $bgrgb[1], $bgrgb[2]); // create a Font object, style (2nd param) is bold (0 = plain, 2 = italic) $font = new Java('java.awt.Font', $fontName, 1, $fontSize); /* Looks weird, but we need a dummy image object to get the * constants of 'java.awt.image.BufferedImage' * Thus, we can also get the FontMetrics and don't need to create a Frame * I haven't found a better solution yet */ $temp = new Java('java.awt.image.BufferedImage', 1, 1, 1); $tempg2d = $temp->createGraphics(); $fontMetrics = $tempg2d->getFontMetrics($font); // calculate the dimensions that fit the message $messageWidth = $fontMetrics->stringWidth($message); $imgWidth = $messageWidth+2*$fontSize; $imgHeight = $fontSize*2; // instantiate a BufferedImage with 8-bit color components $img = new Java('java.awt.image.BufferedImage', $imgWidth, $imgHeight, $temp->TYPE_INT_RGB); $g2d = $img->createGraphics(); // get the Graphics2D object $g2d->setPaint($bgcol); // paint the background $g2d->fillRect(0, 0, $imgWidth, $imgHeight); $g2d->setFont($font); // set the Font for our message $g2d->translate(35, $imgHeight*0.7); // move the text so that it will be centered $g2d->setPaint($shadowcol); // set the color for the shadowed text $affineTransform = $g2d->getTransform(); // get an AffineTransform object $g2d->shear(-1.0, 0); // shear the message by 45 deg to the right $g2d->scale(1, 1.5); // resize, the shadowed text should be 1.5 times higher $g2d->drawString($message, 0, 0); // draw the shadowed text $g2d->setTransform($affineTransform); // apply the transformation // the spicy ingredient: a gradient-painted message in the foreground $gpaint = new Java('java.awt.GradientPaint', 0, 0, $startcol, $imgWidth, $imgHeight, $endcol); $g2d->setPaint($gpaint); $g2d->drawString($message, 0, 0); // create a DataOutputStream for the JPEGEncoder $out = new Java('java.io.DataOutputStream', new Java('java.io.FileOutputStream', $imgName)); // fortunately, we can instantiate the codec directly $codec = new Java('com.sun.image.codec.jpeg.JPEGCodec'); // create the encoder with the given OutputStream $encoder = $codec->createJPEGEncoder($out); $encodeParam = $encoder->getDefaultJPEGEncodeParam($img); // enable best quality, otherwise our JPEG gets blurred $encodeParam->setQuality(1.0, false); $encoder->setJPEGEncodeParam($encodeParam); $encoder->encode($img); // output!!! ?> This is just a short and popular example to proof the concept, but the result is already awesome! By using the classes from the package 'java.awt.image' (Kernel, ConvolveOp, RescaleOp, etc.), you can perform even more advanced and professional image-editing operations. Or use Acme's GIF-encoder instead of Sun's JPEG-encoder.

« previous php.notes (#75803) next »