Hex to image in PHP - php

Any other way to create the image from hexadecimal number?
I use a signature pad to get the signature and save it as an image and put in a PDF;
I have created the image (*.PNG) from the hex code(signiture pad generates the HExadecimal number); the image seems fine (I can open it and see it!), but for some reason this image cannot be put in the PDF by FPDF; however I can put any other images to my PDF by using FPDF; so I guess there is a problem with the image I created (HEX to Image). I created my image by the following code:
$binary = pack("H*", $MyHex);
file_put_contents("../img/Sign_Representative.png", $binary);
Do you know any other way I can create the image from HEX or any way I can handle this problem?
I appreciate your guidance!

Are you sure that signature pad provides PNG-data in HEX?
Check generated file's content if the first row contains letters "PNG" (without quotes).
Tried yours and following and all provided data correctly back:
// test 1
$binary = pack("H" . strlen($MyHex), $MyHex);
file_put_contents("../img/Sign_Representative-1.png", $binary);
// test 2
$binary = hextobin($MyHex);
file_put_contents("../img/Sign_Representative-2.png", $binary);
// #src http://www.php.net/manual/en/function.hex2bin.php#110973
function hextobin($hexstr)
{
$n = strlen($hexstr);
$sbin="";
$i=0;
while($i < $n) {
$a =substr($hexstr,$i,2);
$c = pack("H*",$a);
if ($i == 0) {
$sbin = $c;
} else {
$sbin .= $c;
}
$i += 2;
}
return $sbin;
}
btw, what kind/model signature pad you have? i.e. Honeywell TT8500?

Related

Error when converting base64 to image file in PHP

I get an error when I convert base64 to an image file, it creates an image file but when I open it, it is an error image file?
In the image error message: Looks like we don't support this file format
My code is as follows
public function Base64ToImg($str,$ouput){
$bs64 = (strpos($str, ",")) ? explode(',',$str)[1] : $str;
$bin = base64_decode($bs64);
$size = getImageSizeFromString($bin);
$ext = substr($size['mime'], 6);
$ouput = $ouput.".$ext";
file_put_contents($ouput,$bin);
return $ouput;
}
UPDATE
The error is not in my code but because I am sending base64 code the image via POST method but in my base64 code contains + character so when sending via POST it will be removed + character goes error, I used js code logo = logo.replaceAll("+","*"); to convert character + to * and PHP code $logo = str_replace("*", "+", $logo); to convert * back to +, and it worked fine for me

Repeating watermark php

I am using Intervention Image image manipulation library this library in a project and I am stuck on adding watermark image all over the source image.
Is it possible to repeat the watermark image on all over the source image like in the following example?
I try the following code, but it doesn't work for me.
$thumbnail = $manager->make($name);
$watermark = $manager->make($watermarkSource);
$x = 0;
while ($x < $thumbnail->width()) {
$y = 0;
while($y < $thumbnail->height()) {
$thumbnail->insert($watermarkSource, 'top-left', $x, $y);
$y += $watermark->height();
}
$x += $watermark->width();
}
$thumbnail->save($name, 80);
I've just solved this problem via Intervention Image library using it in Laravel Framework. So here's code snippet.
public function watermarkPhoto(String $originalFilePath,String $filePath2Save ){
$watermark_path='photos/watermark.png';
if(\File::exists($watermark_path)){
$watermarkImg=Image::make($watermark_path);
$img=Image::make($originalFilePath);
$wmarkWidth=$watermarkImg->width();
$wmarkHeight=$watermarkImg->height();
$imgWidth=$img->width();
$imgHeight=$img->height();
$x=0;
$y=0;
while($y<=$imgHeight){
$img->insert($watermark_path,'top-left',$x,$y);
$x+=$wmarkWidth;
if($x>=$imgWidth){
$x=0;
$y+=$wmarkHeight;
}
}
$img->save($filePath2Save);
$watermarkImg->destroy();
$img->destroy(); // to free memory in case you have a lot of images to be processed
}
return $filePath2Save;
}
If you use PHP version prior to 7 remove String type declaration from function arguments. just make it
public function watermarkPhoto($originalFilePath, $filePath2Save ){....}
Also if you are not using Laravel Framework and you don't have File class included just remove redundand check from function.
if(\File::exists($watermark_path))
So the simplest framework-agnostic function would be:
function watermarkPhoto($originalFilePath, $filePath2Save ){
$watermark_path='photos/watermark.png';
$watermarkImg=Image::make($watermark_path);
$img=Image::make($originalFilePath);
$wmarkWidth=$watermarkImg->width();
$wmarkHeight=$watermarkImg->height();
$imgWidth=$img->width();
$imgHeight=$img->height();
$x=0;
$y=0;
while($y<=$imgHeight){
$img->insert($watermark_path,'top-left',$x,$y);
$x+=$wmarkWidth;
if($x>=$imgWidth){
$x=0;
$y+=$wmarkHeight;
}
}
$img->save($filePath2Save);
$watermarkImg->destroy();
$img->destroy();
return $filePath2Save;
}
Also you need watermark image in png format with transparent background.
I'm just adding one thing added to your accepted answer when you try your code and the above-accepted code, the watermark on the images will be very close together and close together with what I tried. like this
So if you want the watermarks like what you want, you need to modify the code with plus numbers on wmarkheight and wmarkwidth like this:
while ($x < $imgWidth) {
$y = 0;
while($y < $imgHeight) {
$imgFileCollection->insert($watermark, 'top-left', $x, $y);
$y += $wmarkHeight+30;
}
$x += $wmarkWidth+40;
}
this line of code is important:
$y += $wmarkHeight+30;
$x += $wmarkWidth+40;
and you will get the result like that below:

Issue getting files in with FPDI/FPDF

I am having issues getting an image into my PDF. I have a form, and on it I have two upload fields. The user can upload a PDF, or an image. When the file is uploaded, it is saved in my temp folder without an extension. I pass my class two things: $fileData, which is the url to the file, and inputArray which is an array of other data from the form (name, address, etc).
My code is like so
private $tplidx;
public function __construct($fileData, $inputArray) {
$pdf = new \FPDI();
$count = 10;
$pdf->AddPage('P');
$pdf->SetFontSize(20);
foreach($inputArray as $input) {
$pdf->SetXY(50, $count);
$pdf->Write(1, $input);
$count = $count + 10;
}
foreach($fileData as $name => $extension){
if($extension == "application/pdf") {
$pagecount = $pdf->setSourceFile($name);
for($i=0; $i<$pagecount; $i++){
$pdf->AddPage();
$this->tplidx = $pdf->importPage($i+1);
$pdf->useTemplate($this->tplidx, 10, 10, 200);
}
} else {
$pdf->AddPage();
$filetype = explode("/",$extension);
$pdf->Image($name.'.'.$filetype[1],30,120,25);
}
}
$pdf->Output('test.pdf', 'F');
}
The first foreach adds the inputs from the field to a page, this works fine.
The next foreach next checks if its a pdf, and if it is, it adds it to another page in the PDF. This also works fine.
My problem is in the else, because I am appending the files extension, I get the error
Can't open image file
If I remove the extension part, I get the error
Image file has no extension and no type was specified
Is there any way to solve this issue?
Thanks
You can pass the image type in the $type parameter of the Image() method.

Replace default image with random image from same folder

I'm just learning php and am stuck on a basic problem. I'd like my site to display one of ten random images (1.png ... 10.png) if a user doesn't choose an image. Currently it just displays a single image by default. There is a thumbnail and full size version of this image.
I've found the file where this is controlled is:
<?php
class Sabai_Helper_NoImageUrl extends Sabai_Helper
{
public function help(Sabai $application, $small = false)
{
$file = $small ? 'no_image_small.png' : 'no_image.png';
return $application->getPlatform()->getAssetsUrl() . '/images/' . $file;
}
}
I've tried to replace 'no_image_small.png' with the names of my images typed out in quotes ('1.png', '2.png',...). Other answers I've seen on stackoverflow are a little too advanced for me to apply as I don't know where to add arrays for both the 'no_image_small.png' and 'no_image.png'.
Thanks very much
I'm not sure what framework you are using, what classes are extended, but try this:
class Sabai_Helper_NoImageUrl extends Sabai_Helper
{
public function help(Sabai $application, $small = false)
{
$random = rand(1, 10);
$file = $small ? $random.'_small.png' : $random.'.png';
return $application->getPlatform()->getAssetsUrl() . '/images/' . $file;
}
}
This assumes, that the images "1.png".."10.png" and "1_small.png".."10_small.png" are located in the same directory, where no_image_small.png and no_image.png are stored.
For example:
$random = rand(1, 10); // generating random number and saving to $random variable
echo $random.'.png'; // concatenating random number to .png extension
Outputs:
8.png
or any other from 1 to 10
This should set $file variable with the correct image.
Reference: rand
Try using php rand()
You can use rand(1,10) this means echo out a random numbers from 1 to 10.
like for example: echo rand(1,10)."jpg";

How can I obtain the dimensions of a Flash video file from PHP code?

I have an application where users can upload video files of any size, and I'd like to be able to determine the the height/width of a Flash video file (flv or f4v) from a PHP script so that I can size the player appropriately. I'd love a pure PHP solution, but I'd be open to shelling out to a command line tool and parsing the output (if such a tool exists).
Thanks in advance!
ffmpeg is probably your best bet, there is even a php module of it.
ffmpeg -i "FileName"
Alternativly you could read the information from the flv file directly by opening the file and reading the meta information.
If you can't use ffmpeg because you don't have control over your server or if you want a PHP solution, have a look at getID3, there's a FLV module that should return a resolution.
There's also flv4php.
If the file's header contains the video's dimensions (which might not always be the case), you can also use the following code:
function flvdim($name) {
$file = #fopen($name, 'rb');
if($file === false)
return false;
$header = fread($file, 2048);
fclose($file);
if($header === false)
return false;
return array(
'width' => flvdim_get($header, 'width'),
'height' => flvdim_get($header, 'height')
);
}
function flvdim_get($header, $field) {
$pos = strpos($header, $field);
if($pos === false)
return false;
$pos += strlen($field) + 2;
return flvdim_decode(ord($header[$pos]), ord($header[$pos + 1]));
}
function flvdim_decode($byte1, $byte2) {
$high1 = $byte1 >> 4;
$high2 = $byte2 >> 4;
$low1 = $byte1 & 0x0f;
$mantissa = ($low1 << 4) | $high2;
// (1 + m·2^(-8))·2^(h1 + 1) = (2^8 + m)·2^(h1 - 7)
return ((256 + $mantissa) << $high1) >> 7;
}
Pleaso note that the code is reverse engineered from binary files, but it seems to work reasonably well.
Alternatively, Tommy Lacroix seems to have an interesting solution too for reading the metas out of FLV files.
I've heard that you can use regular getimagesize() but i haven't tested it. Give it a try.

Categories