UniHelp-Blog von schnueptus

Die Welt ist groß!

Mittwoch, 3. Dezember 2008

real transparent Thumbnails with Imagemagick and reflection effect

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:
Bild-Anhang zum Blog-Eintrag

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:
Bild-Anhang zum Blog-Eintrag

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:
Bild-Anhang zum Blog-Eintrag

Install php5-imagick on your system and it will work.

geschrieben von schnueptus um 17:43, 12 Kommentare, 0 Trackbacks, in Web,

Kommentare

  • #1 (extern) e-thereal scripsit, 16.12.2008, 07:35:

    Warum bloggst du eigentlich auf Englisch?

  • #2 (intern) schnueptus scripsit, 16.12.2008, 09:16:

    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. :wave:

  • #3 (intern) Alita scripsit, 24.12.2008, 08:40:

    Na da bin ich mal gespannt, ob die Firma in die du mal kommst, das später genauso sieht ;-)

  • #4 (intern) scripsit, 28.12.2008, 21:36:

    egal ob englisch oder deutsch das produkt zählt und kann sich ja sehen lassen :)

  • #5 (extern) Tom scripsit, 12.11.2009, 20:56:

    What i have to do for getting your result ?

  • #6 (intern) schnueptus scripsit, 15.11.2009, 15:13:

    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.

  • #7 (extern) TfKing scripsit, 20.02.2010, 03:16:

    Thanks a lot man, i was looking for this code.
    You save me a lot of time !!! :hug:

  • #8 (extern) sunglasses shop scripsit, 28.05.2010, 04:53:

    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

  • #9 (extern) Simon scripsit, 08.06.2010, 09:19:

    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.

  • #10 (extern) Simon scripsit, 08.06.2010, 09:23:

    ... 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:

    [..]
    $reflection->compositeImage( $gradient, imagick::COMPOSITE_DSTOUT, 0, 0);

    $im->addImage($reflection);

    $im->resetIterator();
    $im = $im->appendImages(true);
    echo $im;
    [...]

  • #11 (intern) schnueptus scripsit, 08.06.2010, 09:55:

    Wie wäre es, wenn du von $reflection noch die Helligkeit manipulierst? Kannst du mit modulateImage() glaube ich machen.

  • #12 (extern) Simon scripsit, 08.06.2010, 10:10:

    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();
    $opacity->newImage( $reflection->getImageWidth(), $reflection->getImageHeight(), new ImagickPixel('black'));
    $opacity->setImageOpacity( 0.7 );
    $reflection->compositeImage( $opacity, imagick::COMPOSITE_DSTOUT, 0, 0);

Kommentar hinzufügen





Smileys

alle Smileys