I just searched for a way to have a serverside Thumbnail generation in PHP with a reflection effect on it.
This is my test image:
First i found Easy Reflections v3 by Richard Davey. But here is the problem that this uses GD and the function imagelayereffect. This is part of the bundle version. If you want to use it with Debian/Ubuntu, you have to compile it on your own. This is not the best choice if you administrate your system and you are worried about security reasons.
Result with fake:
Later i found another PHP-Script in Mikko's Blog. A guy developing Imagemagick. The Problem is that this has no real transparents only with fake black gradient.
But i changed this source and added real transparency to the png output. If you have an image with transparent background.
Hier is the Script:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 | <?php /* Read the image */ $im = new Imagick( "images/produkte/pic1.png" ); /* Thumbnail the image */ $im->thumbnailImage( 200, null ); /* Create a border for the image */ //this has to be removed, it destroys the transparency in our image //$im->borderImage( "white", 5, 5 ); /* Clone the image and flip it */ $reflection = $im->clone(); $reflection->flipImage(); /* Create gradient. It will be overlayd on the reflection */ $gradient = new Imagick(); /* Gradient needs to be large enough for the image and the borders */ $gradient->newPseudoImage( $reflection->getImageWidth() + 10, $reflection->getImageHeight() * 0.5, "gradient:transparent-black" ); /* Composite the gradient on the reflection */ // change the style of adding $reflection->compositeImage( $gradient, imagick::COMPOSITE_DSTOUT, 0, 0 ); // i just want to have half of the image as reflection $gradient->newPseudoImage( $reflection->getImageWidth() + 10, $reflection->getImageHeight() * 0.5, "gradient:black" ); $reflection->compositeImage( $gradient, imagick::COMPOSITE_DSTOUT, 0, $reflection->getImageHeight() * 0.5 ); /* Add some opacity */ // now we have real transparency so we don't need this any more //$reflection->setImageOpacity( 0.3 ); /* Create empty canvas */ $canvas = new Imagick(); /* Canvas needs to be large enough to hold the both images */ $width = $im->getImageWidth(); $height = ( $im->getImageHeight() * 1.5 ) ; //the new image has no background $canvas->newImage( $width, $height,'none', "png" ); /* Composite the original image and the reflection on the canvas */ //change the style of adding $canvas->compositeImage( $im, imagick::COMPOSITE_SRCOVER, 0, 0 ); $canvas->compositeImage( $reflection, imagick::COMPOSITE_SRCOVER, 0, $im->getImageHeight() ); /* Output the image*/ header( "Content-Type: image/png" ); echo $canvas; ?> |
my result:
Install php5-imagick on your system and it will work.













Warum bloggst du eigentlich auf Englisch?
Weil die Seiten auf die ich mich beziehe auch auf Englisch sind und ich eigentlich auch keinen Code in Deutsch entwickle, da Englisch einfach die Sprache ist in der man Software schreibt und kommentiert.
Na da bin ich mal gespannt, ob die Firma in die du mal kommst, das später genauso sieht
egal ob englisch oder deutsch das produkt zählt und kann sich ja sehen lassen
What i have to do for getting your result ?
Copy my Script to the Server with install PHP, change the path of the original in line 2 and execute the script with the browser.
Thanks a lot man, i was looking for this code.
You save me a lot of time !!!
I really enjoyed this post, especially the “examples in this post” portion which made it really easy for me to SEE what you were talking about without even having to leave the article. Thanks
Danke für das Tutorial, hat mir sehr weiter geholfen. Eine Frage: wie kann man denn die Reflektion etwas absoften? Mit "setImageOpacity" wird das ganze dann leider wieder un-transparent.
... und hier noch was produktives zur Fragerei: den unteren Teil mit dem Canvas kann man auch ein klein wenig vereinfachen, indem man kein neues Bild erzeugt, sondern es zum bestehenden hinzufügt:
[..]
COMPOSITE_DSTOUT, 0, 0);
$reflection->compositeImage( $gradient, imagick
$im->addImage($reflection);
$im->resetIterator();
$im = $im->appendImages(true);
echo $im;
[...]
Wie wäre es, wenn du von $reflection noch die Helligkeit manipulierst? Kannst du mit modulateImage() glaube ich machen.
Danke, da macht er irgendwas seltsames mit der Transparenz. Ich habe es jetzt aber folgendermaßen gelöst, indem einfach noch ein zusätzliches Bild mit Schwarz fülle, bei diesem die Transparenz ändere und es dann mit der Spiegelung kombiniere. Geht sicher auch einfacher, aber - es funktioniert:
$opacity = new Imagick();
COMPOSITE_DSTOUT, 0, 0);
$opacity->newImage( $reflection->getImageWidth(), $reflection->getImageHeight(), new ImagickPixel('black'));
$opacity->setImageOpacity( 0.7 );
$reflection->compositeImage( $opacity, imagick