What I want to do is have a new image generated when the page is loaded. I have the random string working, but the image won't generate. If I assign it a normal name, not a variable, it'll generate perfectly fine, permissions are set, etc.. Not exactly sure what to do to get it to create the image
Here's the code that works.
$chart->render("generated/chart.png");
// But it isn't dynamic, like I want it to be
print "<center><img src=generated/$char.png></center>";
Here's the code that doesn't work. So I had the idea of using a random string generator to make a random name for the image, and just do that.
// random string code
$length = 10;
$randomString = substr(str_shuffle("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"), 0, $length);
$chart->setTitle("Mini Mood Map");
// THIS is the line giving me problems, I've tried different quotes with no luck
$chart->render(generated/$randomString.png);
// Broken image
print "<center><img src=generated/$randomString.png></center>";
Sorry, I have a bad cold and my heads not on tight tonight. Thanks for all suggestions.
Note how the original is quoted:
$chart->render("generated/chart.png");
You need to do the same:
$chart->render("generated/" . $randomString . ".png");
Related
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.
Can someone suggest a complex algorithm in php to name files that would be uploaded so that it never repeats? i wonder how youtube which has millions of videos does it??
Right now i use an random number and get its 16 character sha1 hash and name the file with that name but i'm pretty sure it will eventually repeat and generate an error as file will not be able to save in the file system.
something like:
$name = sha1(substr(sha1(md5($randomnumber)),0,10));
somebody once told me that its impossible to break the hash generated by this code or at least it'll take 100 years to break it.
you could do:
$uniq = md5(uniqid(rand(), true));
You could also apped user id of users uploading the file, like:
$uniq = $user_id_of_uploader."_".md5(uniqid(rand(), true));
Generate a GUID (sometimes called UUID) using a pre-existing implementation. GUIDs are unique per computer, timestamp, GUID generated during that timestamp and so on, so they will never repeat.
If making a GUID isn't available, using sha1 on the entire input and using the entire output of it is second best.
$name = 'filename'.$user_id(if_available).md5(microtime(true)).'extension';
Try to remove special characters and white spaces from the file name.
If you are saving name in database then a recursive function can be helpful.
Do below with proper methods.
First slice its extension and filename
Now Trim the filename
Change multiple Space into single space
Replace special character and whitespace into to _
Prefix with current timestamp using strtotime and salt using md5(uniqid(rand(), true)) separated by _ (Thanks to #Sudhir )
Suffix with a special signature using str_pad and limit the text length of a file
Now again add extension and formatted file name
hope it make sense.
Thanks
I usually just generate a string for the filename (implementation is not incredibly important), then check if a file already exists with that name. If so, append a counter to it. If you somehow have a lot of files with the same base filename, this could be inefficient, but assuming your string is unique enough, it shouldn't happen very often. There's also the overhead of checking that the file exists.
$base_name = generate_some_random_string(); // use whatever method you like
$extension = '.jpg'; // Change as necessary
$file_name = $base_name . $extension;
$i = 0;
while (file_exists($file_name)) {
$file_name = $base_name . $i++ . $extension;
}
/* insert code to save the file as $file_name */
I'm working with a function taken from Corrupt (a web based piece of software used to get "glitchy" effects using jpeg images). This function can be found in the corrupt.php file on line 23. At the moment it's not making the files glitchy enough. I made this images to show you how I want the images to look. This was made by opening the jpeg in a text editor and cutting certain lines and pasting them in other places.
I want this function to do a similar thing but at the moment it doesn't. Any ideas? Is there a better way of doing this maybe?
function scramble($content, $size) {
$sStart = 10;
$sEnd = $size-1;
$nReplacements = rand(1, 30);
for($i = 0; $i < $nReplacements; $i++) {
$PosA = rand($sStart, $sEnd);
$PosB = rand($sStart, $sEnd);
$tmp = $content[$PosA];
$content[$PosA] = $content[$PosB];
$content[$PosB] = $tmp;
}
return($content);
}
It is randomly swapping information around in the data arrays loaded from your image. This causes a valid image to come out with invalid image information in some sectors. Also, image files sometimes contain additional information at the front/end of the file; this does not look like it takes that into account and could corrupt that information as well.
To increase the amount of swaps you will want to increase the number of replacements. The bit of code you are particularly interested in is rand(1, 30);; I would suggest increasing the minimum amount of scramble first and then the upper range if you still do not get the desired effect.
The function does random swaps between the elements of the array. The number of swaps is a randomly generated number from 1 to 30.
I'm using simple php script to count visits. It's saved inside file. So there is a number inside a .txt file and I would like to put on my page in a form of some nice counter. So I need numbers to be represented as images in some way. So it can look nice. If I eventually had div where I can show this numbers, and 10 images of numbers from 0 to 9. 25x50px lets say and I would like to put them into tags is that possible?
I know my question is a bit dodgy and maybe not clear. But if someone understand what I'm asking for than if you could answer that would be great. Thx
Split the number into digits:
$a = str_split(1337, 1);
Use the array to create the counter:
foreach ($a as $n) {
print '<img src="'.$n.'.png">';
}
To get the number from the file:
$number = file_get_contents('visitors.txt');
To create an image, use the GD or Imagick library:
header("Content-type: image/png");
$im = imagecreatefrompng("images/button1.png");
// Add text to $im
imagepng($im);
To include your image in a page:
<img src="myImageScript.php?number=1234" />
Yes it is possible. First you need to get the number in char array and the proceed. Based on number display the images nest to each other.
If your problem is only showing the number as images, it's easy. Do something like:
$num = 1234;
for ($i = 0; $i < strlen($num); $i++)
echo "<img src=\"images/numbers/" . $num.{$i} . ".png\">";
have 10 images for the numbers 0-9 (0.png, 1.png, ...) and just convert every character in a string $counter="12345" into a corresponding image <img src="1.png" />
this is explained and implemented in javascript on this website.
you may also want to str_pad your $counter with 0.
I need to save many variables in text file and later retrieve any one random data from the text document.
For Example ...i need to add 31231231 to the below text file
213123123
213123124
123412321
and i need to retrieve random value from above text file ..for example 213123124 from above text file
To write to the already existing file :
file_put_contents("file.txt","\n31231231",FILE_APPEND);
To get a random value from the file :
$file = file("file.txt")
$len = count($file);
$rand = rand ( 0, $len-1 );
echo $file[$rand];
Edit: While retrieving the PHP_EOL is included so do this:
echo intval($file[$rand]);
Edit: Seeing as intval() returns the max_int for numbers that exceed int specs, just use trim();
echo trim($file[$rand])
To save and access variables and information when stored as text, in db or flat files, create array of the data first, then use serialize() on the data before saving, and unserialize() after fetching again. This allows for easy handling of information.
And remember to save many smaller files , rather than a large one...
To write in to a file you can use :
file_put_contents("test.txt","31231231".PHP_EOL ,FILE_APPEND);
Here PHP_EOL outputs \r\n or \n depending on the OS.
To select a random value from the text file you can use:
$file = file("test.txt")
$file_length = count($file);
$random_value = rand ( 0, $file_length-1 );
echo $file[$random_value];
New Edit:
If you want to avoid new line at the retrieved output you can do :
echo intval($file[$random_value]);
Hope this helps