Text/symbols used in ImageString not showing correctly - php

I am having a few issues, and have no idea how to work around it and it's been bugging me for days.
I have a script that creates demotivational posters.
(example: http://www.motifake.com/image/demotivational-poster/0901/hey-beetlejuice-doris-joker-funny-demotivational-poster-1231432214.jpg)
The issue I am faced with is the TEXT that is inputted with symbols like: !#£$%^&*(),.~'
They never show, or come back with different symbols to what they should be, Like a \
will be \\ and a ' will be '\.
I have no special formatting the way the code is used is as follow:
This is the input field for the text:
Motivational text: <input type="text" name="motivationaltext">
In my PHP file I use:
$motivationaltext = $_POST['motivationaltext'];
And later on I use this for the image string:
CenterImageString($im, $width, $motivationaltext, 16, ($top + $imgy + 74), $text_color);
Does anyone have a idea on how to go about this?

In your php.ini file, set magic_quotes_gpc = Off. This should take care of the extra \.
Alternate solution: if the setting is on and you cannot (or do not want to) turn it off, use the stripslashes() function:
$motivationaltext = stripslashes($_POST['motivationaltext']);

Related

Using GetStringWidth in TCPDF to short a string

I'm using TCPDF to print a Barcode sheet label.
Each label has a barcode and some text underneath.
Evreything seems to work fine, but somtimes the text is to long and 'invade' the next label/next line.
I'm trying to check the length of the string - and short it if needed:
$label_w = ($page_w-$right_mar-$left_mar)/$Col;
$text_width = $pdf->GetStringWidth($exploded_line[2]);
while ($text_width>$label_w-15) // "-15" because the text location
{
$exploded_line[2]=substr($exploded_line[2],0,-1);
$text_width = $pdf->GetStringWidth($exploded_line[2]);
}
The text that going into the loop just continue shrinking until only the first letter left...
At first I thought that the problem is that my While condition isn't stopping for some reason.
Then I tried changing it to simple if - BUT the problem isn't gone...
if ($text_width>$label_w-15)
{
$exploded_line[2]=substr($exploded_line[2],0,-1);
$text_width = $pdf->GetStringWidth($exploded_line[2]);
}
Any suggestions? Thanks.
OK, finally I got it.
The problem was really in the substr function. I'm using UTF-8, so I had to use mb_substr...
$exploded_line[2]=mb_substr($exploded_line[2],0,-1,"utf-8");
This is working as expected.
Thanks anyway.

Code utilizing GD ending up in an error that "image cannot be displayed"

My code is as follows:
<?php
session_start();
$img=imagecreatetruecolor(150,50);
$white=imagecolorallocate($img,255,255,255);
$black=imagecolorallocate($img,0,0,0);
$red=imagecolorallocate($img,255,0,0);
$pink=imagecolorallocate($img,200,0,150);
$grey=imagecolorallocate($img,150,150,150);
$blue=imagecolorallocate($img,0,204,255);
$redd=imagecolorallocate($img, 153, 0,0);
function randomString($length){
$chars="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ023456789";
srand((double)microtime()*1000000);
$str="";
while($i<=$length){
$num=rand() % 33;
$tmp=substr($chars,$num,1);
$str.=$tmp;
$i++;
}
return $str;
}
for($i=0;$i<=rand(1,5);$i++)
{
$color=(rand(1,2)==1)? $grey:$white;
imageline($img, rand(5,50),rand(5,50),rand(50,150) , rand(5,50), $color);
}
$ran=randomString(rand(3,6));
$_SESSION['captcha']=$ran;
imagefill($img,0,0,$redd);
imagettftext($img,14,7,23,27,$black,"fonts/times_new_yorker.ttf",$ran);
imagettftext($img,16,10,18,30,$white,"fonts/times_new_yorker.ttf",$ran);
header("Content-type:image/png");
imagepng($img);
imagedestroy($img);
?>
Yesterday this worked as expected. But now Firefox is showing a message:
This image cannot be displayed, because this contains error.
When I searched for any solutions, it seems everyone is saying something about enabling GD. But in my code GD is enabled, and this very code worked perfectly up until this morning.
Can anyone help me to get a solution for this?
The image cannot be displayed, because PHP reports an error, and the header('Content-Type: image/png') tells it to show the page as an image.
To see the error, you should remove the following part:
header("Content-type:image/png");
imagepng($img);
imagedestroy($img);
or better yet, surround it with if (!isset($_GET['debug'])) statement. That way you can append ?debug=1 to your URL and see all possible PHP errors, while the image stills display normally.
There are a few possible solutions why your code might have stopped working without changing it. My guess is that you tampered with environment somehow.
session_start() needs to store session data in a directory on your local drive. Does your PHP have access to that directory?
The font fonts/times_new_yorker.ttf could disappear.
You could have moved the script to Linux machine, where letter casing matters. Are you sure the path to the font shouldn't have uppercase characters anywhere in it?
Also, just a couple of tips:
You don't need to call srand(), it's initialized automatically. (I assume you come from C/C++ background).
Instead of using rand(), you should use mt_rand() as it's faster and provides better randomness.
Instead of using magic numbers, you should use meaningful expressions (for example, replace % 33 with % strlen($chars)).
Since you seem to display a captcha, consider matching 0 and O, 1 and l as the same "character", so that reasonable user mistakes are forgiven. (Pardon if you do it already.)

tcpdf and locales getnumlines wrong count

I found annoying error with TCPDF library. Hope you know how help me. I use obj->getNumLines method to calculate how many lines has my text. It works all good in most cases but unfortunatelly not always.I use UTF8 encoding and I use many local signs (I'm from poland and we have a lot of such signs). getNumLines sometimes returns i.e. 5 although multicell displays 6 lines of text.
In my research I found that:
$font = $pdf->addTTFfont('/fonts/calibri.ttf', '', '', 32);
$pdf->setFont($font, '', 13)
$string = 'zażółć gęślą jaźń'; (repeated many times)
echo $pdf->getNumLines($string, 0);
It prints to the screen different (smaller) number than multicell actually returns lines to the pdf file. I use custom fonts as you can see above, margins are not changed during the code.
Does anybody has idea what is wrong or how to fix it?

Merging RGB channels back into a single image with ImageMagick (php)

So I have a situtation with ImageMagick and php where I need to processes each of the RGB channels separately and then merge them back together into the final image. So in the code I have below, $red, $green and $blue are the channels (as gray scale images). The following code is what I have tried (and a couple variations) but each time I end up with an image that only has Cyan, Magenta or Yellow showing through. In this case, the resulting image is Cyan.
$im->removeImage();
$im->addImage($red);
$im->addImage($green);
$im->addImage($blue);
$img = $im->combineImages(self::CHANNEL_ALL);
$im->removeImage();
$im->removeImage();
$im->removeImage();
$im->addImage($img);
I think part of my problem is that the PHP documentation doesn't say much about how to use combineImages and there are no samples so far as I can find. So it's very likely that I'm using that particular method incorrectly, and I suspect it has to do with how I am combining the images in the single Imagick object to begin with.
EDIT
This question ultimately boils down to this: How do I recreate the following script using only php?
convert tmp_r.png tmp_g.png tmp_b.png -combine tmp_rgb.png
[EDIT]
I have to admit, looking further into the documentation, Im not sure what the constant CHANNEL_ALL does. They do state that you can concatenate channels by logically ORing them together. You might try:
$im->combineImages(imagick::CHANNEL_RED | imagick::CHANNEL_GREEN | imagick::CHANNEL_BLUE);
[ORIGINAL]
I've been looking into this API, and honestly what I think you are looking for is the convert function, NOT the combine function.
Look At the below provided link and click specifically on "Combining RGB Channel Images"
http://www.imagemagick.org/Usage/color_basics/
Try that, leave a comment if you need further help :-)
So I think I've figured out how to get this to work. The missing piece was a call to flattenImages(). I'm not exactly sure why this worked, but it seems to be what I was looking for. Here's the code (keep in mind that $this is in the context of a member method of a class that extends Imagick):
$this->removeImage(); // gets rid of the old, unprocessed image
$rgb = clone $this;
$rgb->addImage($red);
$rgb->addImage($green);
$rgb->addImage($blue);
$rgb->flattenImages(); // this is what was missing above
$rgb = $rgb->combineImages(self::CHANNEL_ALL);
$this->addImage($rgb);
Can anyone comment on why this might be? I expected flattenImages() to merge the three images into one and destroy some of the information, but it appears that it actually tells ImageMagick to process all of the contained images together whereas it was processing them independently previously.
Try this:
$im->addImage($red);
$im->addImage($green);
$im->addImage($blue);
$im->combineImages(imagick::CHANNEL_RED | imagick::CHANNEL_GREEN | imagick::CHANNEL_BLUE);
btw combineImages doesn't return imagick object, but true/false indicating success or failure, so $im will contain your combined image.
Edit:
Apparently combineImages sucks big time, so here's an alternative: imagick::compositeImage
$im->compositeImage($red, imagick::COMPOSITE_COPY, 0, 0, imagick::CHANNEL_RED);
$im->compositeImage($green, imagick::COMPOSITE_COPY, 0, 0, imagick::CHANNEL_GREEN);
$im->compositeImage($blue, imagick::COMPOSITE_COPY, 0, 0, imagick::CHANNEL_BLUE);

Lamp / Cakephp: Streaming an image : Binary 0x00 replaced by 0x20

I'm trying to create a script that pulls an image out of the database and displays it to the user, called by <img src="viewImage/someImageName">
But the problem I'm having is when the image is displayed all of the Nulls (0x00) are replaced by 0x20 and I have no idea why. The data in the database shows it being nulls but somewhere along the way it gets changed to 0x20s.
Does anyone have any idea? is there something I'm missing?
Here is the code I'm using:
$data = $this->Image->read(NULL, $userId);
header("Content-Type: image/jpeg");
echo($data['image']);
die;
I don't think it has anything to do with the code because as you can see there is no place for error. I can dump the binary contents out and it has not yet been tampered.
Something with the stack or cakephp any thoughts?
Update:
I've noticed that a space is making to the beginning of stream, I'm trying to track it down, could this be the problem?
Yeah, something along the way is freaking out (because OMG nulls, what if something thinks they're string terminators) and replacing them with spaces. I suspect CakePHP but am not quite certain enough to say j'accuse. Try:
header('Transfer-Encoding-Type: base64');
and see if that convinces whatever's doing it to leave your data alone.
I had a stray space in a file somewhere, lots of fun to track down :)
I guess this switches the mode of something in the stack and corrupts the files

Categories