Drop shadow on text - php

I am looking to add drop shadow to text on an image using PHP.
I am aware on how to add text to images and how some libraries allow you to add block shadowing, but I cannot see any which allow you to add a faded drop shadow.
Is this possible?

What you want is Imagick::shadowImage ( float $opacity , float $sigma , int $x , int $y )
Here's an example where I put a drop shadow on some text and then superimpose that on a background image...
$background_layer = new Imagick('poster_pic_01.jpg'); # background image
$text_layer = new Imagick('transparent400.png'); # empty transparent png of the same size
$text_layer->annotateImage( $ImagickDraw, $pad_left, $pad_top, 0, "Your text here" );
/* create drop shadow on it's own layer */
$shadow_layer = $text_layer->clone();
$shadow_layer->setImageBackgroundColor( new ImagickPixel( 'black' ) );
$shadow_layer->shadowImage( 75, 5, 5, 5 );
/* composite original text_layer onto shadow_layer */
$shadow_layer->compositeImage( $text_layer, Imagick::COMPOSITE_OVER, 0, 0 );
/* composite shadow_layer (which now has text AND the shadow) onto image_layer */
$background_layer->compositeImage( $shadow_layer, Imagick::COMPOSITE_OVER, 0, 0 );
Hope this helps,
Roger

GD can't do this out of the box. If you can, use ImageMagick. Examples on how to do shaped shadows here.

Related

How to set custom page size in fpdf php library?

I need some help with fPDF. I want to set up my custom page size (exactly: width 3 inch, and height 5 or 6 inch).
it will create number of pages again height parameter .
i set the size array(3,5). it will create 5 page. I
found fPDF() manual (http://www.fpdf.org/) but there are only ready formats like A4, B5 etc. I have to set up my own page format.
<?php
require_once('fpdf/fpdf.php');
//$fromat = array(3,5);
$pdf = new FPDF('p','in', [4.1,2.9]);
$pdf->SetTopMargin(50);
$pdf->Addpage();
$pdf->SetTitle("invoice");
$pdf->SetCreator("maqbool solutons");
$pdf->SetAuthor("my name");
$pdf->SetSubject("report");
$pdf->SetFont('Arial', 'B', '16');
$pdf->SetTextColor(155,14,9);// rgb
$pdf->SetDrawColor(155,14,9);
$pdf->SetfillColor(15,140,95);
$pdf->Cell(60,10, 'hello word');
$pdf->Cell(60,10,'powered by fpdf', 1, 0,'c',true);
$pdf->Cell(60,10,'powered by fpdf', 1, 2,'c');
$pdf->Cell(60,10,'powered by fpdf', 1, 1,'c');
$pdf->Image("images/coat.jpg", 10,20,10,35);
$pdf->MultiCell(94,10,"skldjfsldfsfjsdkfsjdlfjsdflkjsdflksjflksjdflskjfslkjfdslkfdjslkfdjslkfjslkfjslkfjsflkjsflkjsflksjflksjfslkjfslkjslkf",1,"L",false);
$pdf->Output("I", "invice.pdf");
?>[that is my file size][1]
when i add array of size
You should should define it in your constructor like so:
$pdf = new FPDF('P','in',[3,6]);
You can find more info in tutorial #1 and in the manual > AddPage
As said in the documentation, when you call the constructor or AddPage, you can either give a String or an Array containing the width and height:
// AddPage([string orientation [, mixed size [, int rotation]]])
$pdf->AddPage("P", [3, 5]); // assuming you are using 'in' as unit
Or directly using the constructor:
// __construct([string orientation [, string unit [, mixed size]]])
$pdf = new FPDF('P','in',[3, 5]);
I think you can set the page size with the constructor.
I have not tested it but this should show you the way:
$format=array(3,5);
$pdf=new FPDF('P','in',$format);
$pdf->Open();
....

How to rotate full page with tcpdf?

I am using TCPDF library to write a custom size label with background image and multiple text blocks on it.
The user when sees the preview on screen of the PDF it should show in horizontal, but for printing, I need the full page rotated -90 degrees.
How can I just rotate the whole page for printing version without having to move anything?
Basically:
In my case I've already had to use a new document format for the special sizes my document required.
So I've duplicated that format, created one for Landscape and one for Portrait.
Then based on the $preview variable, if previewing I'm rendering the normal landscape document, but if not previewing, I'm using the Portrait format and orientation and also starting the transformation and rotating everything on page.
Hope this helps someone I've found no other "quick" way to accomplish this kind of full-page rotation.
<?php
// #1 get the preview attribute from
// the form that was submitted from the user
$preview= isset($_POST['preview'])?(int)$_POST['preview']:0;
// load TCPDF for CodeIgniter as a library
$this->load->library('Pdf');
// #2 set default orientation and format
$orientation='L';
$format='MAKE-L';
// #3 if not previewing, switch orientation and format to portrait
if (!$preview) {
$orientation='P';
$format='MAKE-P';
}
// create new pdf object
// (same as doing new TCPDF(), it is just the CodeIgniter wrapper)
$pdf = new Pdf($orientation, 'mm', $format, true, 'UTF-8', false);
// remove default header/footer
$pdf->setPrintHeader(false);
$pdf->setPrintFooter(false);
$pdf->SetMargins(0, 0, 0);
$pdf->AddPage($orientation, $format);
// #4 if not previewing, start transformation
// and rotate everything before inserting any content
if (!$preview) {
// Start Transformation
$pdf->StartTransform();
// Rotate counter-clockwise centered by (x,y)
$pdf->Rotate(-90, 70, 70); // <-- TODO: test this very well because 70 and 70 was just guessing, there is no math behind that so not sure if it will work always
}
// put your content here,
// for example set font and add a text
$pdf->SetFont('times', '', 7, '', true);
$pdf->writeHTMLCell(0, 0, 25.4, 2, 'lot number', 0, 1, 0, true, '', true);
/// end content
// #5 if not in preview mode, finish the transformation
if (!$preview) {
// Stop Transformation
$pdf->StopTransform();
}
$pdf->Output('example.pdf', 'I');
/**
* Last but very important note:
* I have added my formats in tcpdf/includes/tcpdf_static.php file.
* >> MAKE-L for Landscape
* >> MAKE-P for Portrait
*/
public static $page_formats = array(
// Make
'MAKE-L' => array( 396.850, 425.196), // = ( h 140 x w 150 ) mm
// Make
'MAKE-P' => array( 425.196, 396.850 ), // = ( h 140 x w 150 ) mm
// .. rest of formats here ../
);
The setPageFormat() method should do the job. You also can pass the parameter to the $format parameter of AddPage():
$pdf->AddPage($orientation, ['format' => $format, 'Rotate' => -90]);

Transparency with GifCreator php class not working

I try to use the Gifcreator php class to create an animated GIF image from 10 PNG images.
The animated image is created but transparency of original images is lost. I have a black bakground.
The documentation says that the transparency is determinated by first loaded image. The 10 images have a transparent background. I loaded htem in Paint whicj says that it is based on white.
Has anoyone a solution to this ?
Thanks,
The class is here : https://github.com/Sybio/GifCreator
My script here : http://www.egloff.eu/rsmaptest/slideshow.php
The first image here : http://www.egloff.eu/rsmaptest/images/image0.png
The code :
<?php
// Include the class
require_once('./testcreator/GifCreator.php');
// Instanciate the class (uses default options with the addition of width/height specified)
$gif = new GifCreator(0, 2, array(0, 0, 0),550,550);
// Add each frame to the animation
$gif->addFrame(file_get_contents('images/image9.png'), 100, true);
$gif->addFrame(file_get_contents('images/image8.png'), 100, true);
$gif->addFrame(file_get_contents('images/image7.png'), 100, true);
$gif->addFrame(file_get_contents('images/image6.png'), 100, true);
$gif->addFrame(file_get_contents('images/image5.png'), 100, true);
$gif->addFrame(file_get_contents('images/image4.png'), 100, true);
$gif->addFrame(file_get_contents('images/image3.png'), 100, true);
$gif->addFrame(file_get_contents('images/image2.png'), 100, true);
$gif->addFrame(file_get_contents('images/image1.png'), 100, true);
$gif->addFrame(file_get_contents('images/image0.png'), 500, true);
// Output the animated gif
header('Content-type: image/gif');
echo $gif->getAnimation();
?>
I found an answer to my own question, and it might help some others using the same library or other libraries based on the same original work by László Zsidi.
In the class, you have to replace the following part that appears in one or another place in 2 lines :
$Locals_ext = "!\xF9\x04" . chr ( ( $this->DIS << 2 ) + 0 ) .
by this one
$Locals_ext = "!\xF9\x04" . chr ((( $this->DIS << 2 )) | 1 + 0 ) .
That solved my problem and transparency works now OK. i tried in 3 differents classes i've found are all based on the same work.
I hope this can help.

How to implement imagecopy (GD) function by Imagick?

I'm working with imagick and face some problem. I want to composite two images: image01 and image02,image01 is background image,a part of image02 composite on image01. the function just like GD's imagecopy function.
bool imagecopy( resource dst_im, resource src_im, int dst_x, int dst_y,
int src_x, int src_y,int src_w, int src_h )
Copy a part of src_im onto dst_im starting at the x,y coordinates
src_x, src_y with a width of src_w and a height of src_h. The portion
defined will be copied onto the x,y coordinates, dst_x and dst_y.
the question is: how to implement imagecopy function by Imagick?
thanks for your help.
This should do it:
//load files from source
$background = new Imagick(image01_src);
$overlay = new Imagick(image02_src);
//Crop the overlay to the required size
$overlay->cropImage ($new_width,$new_height,$x_offset,$y_offset);
//composite overlay on background
$background->compositeImage($overlay, Imagick::COMPOSITE_OVER, $margin_x, $margin_y);
//save result
$background->setImageFormat("png");
$background->writeImage(new_src);
//clean up
$background->clear();
$background->destroy();
$overlay->clear();
$overlay->destroy();
Use composite, for example:
$large_image->compositeImage($small_image, Imagick::COMPOSITE_OVER, $margin_x, $margin_y);
If you show me the source and final pictures, I can give you the exact code.

Trying to skew an image with PHP's GD Library

I've been looking everywhere to try and find a function to skew an image with php using the GD library. I've read threads where ImageMagick has been suggested but I unfortunately don't have access to that library on my server so I'm forced to use GD.
I'm looking for something where I can specify the source image and destination image and then 4 sets of X and Y coordinates for each corner of the image. So something like this would be ideal:
bool skewImage(resource $src_im, resource $dst_im, int $x1, int $y1, int $x2, int $y2, int $x3, int $y3, int $x4, int $y4)
If anyone has or knows of a function like this or similar that would be awesome, thanks!
The PHP manual is an amazing place. This comment pretty much covers a lot of scenarios. Use the 'Perspective' section. Below example is slightly modified to use the width and height from the image.
$image = new imagick( "grid.jpg" );
$points = array(
0,0, 80,120, # top left
$image->width,0, 300,10, # top right
0,$image->height, 5,400, # bottom left
$image->width,$image->height, 380,390 # bottum right
);
$image->setimagebackgroundcolor("#fad888");
$image->setImageVirtualPixelMethod( imagick::VIRTUALPIXELMETHOD_BACKGROUND );
$image->distortImage( Imagick::DISTORTION_PERSPECTIVE, $points, TRUE );
header( "Content-Type: image/jpeg" );
echo $image;

Categories