saving contents with file put contents - php

i'm trying to get google steetview tiles and save them. But file_get_contents doesn't like my url:
here's my code:
$img = "http://cbk0.google.com/cbk?output=tile&panoid="+$panorama_id+"&zoom=3&x="+$i+"&y='"+$x+"'";
$url = ""+$panorama_id+"Xval"+$i.+"Yval"+$x+".jpg";
file_put_contents($url, file_get_contents($img));

You're using + for concatenation. That's not PHP. You need the dot . operator instead:
$img = "http://cbk0.google.com/cbk?output=tile&panoid=" . $panorama_id . "&zoom=3&x=" . $i . "&y='" . $x . "'";
$url = $panorama_id . "Xval" . $i . "Yval" . $x . ".jpg";
See string operators in the PHP manual.
Alternatively, with double quoted strings, variables within the string will be parsed. See the manual page for the string type.
$img = "http://cbk0.google.com/cbk?output=tile&panoid=$panorama_id&zoom=3&x=$i&y='$x'";
$url = "${panorama_id}Xval${i}Yval${x}.jpg";

Related

Create file with customizeable name [PHP]

I have this code, which will create result.png on my server:
imagepng($image, "'./result/result.png");
Now I have $text , $type , $id
How can I create file with result_$text _ $id _ $type?
Example: ./result/result_123_131_1.png ;
./result/result_Text_1364_3.png
It's very simple:
$name = "./result/result_$text_$id_$type";
//do whatever you do to save the image and use $name as name of the file.
In php the double quotes are used whenever you have variables you want to use in some string literal. What I do above is I insert the value of the variables into the string literal "./result/result___".
More info here.
$image = imagepng($image, "'./result/result");
$file = $image . "_" . $text . "_" . $id . "_" . $type ."png";

If not variable condition is not working

I'm trying to check whether the variable value is false or true, here is my code
$print_ready_flag = $_GET['print_ready']; // i'm passing it as either true or false
echo $print_ready_flag ; // I'm getting correct value
What i'm trying to do is,
if ($print_ready_flag == false) {
file_name = "albumpdf_" . $Order['products_name'] . "_" . $OrderId . "_" . $OrderPId . ".pdf";
}
when doing like above expression is evaluating, But when i tried
if (!$print_ready_flag) {,
if (!(bool)$print_ready_flag)
. expression is not evaluating. Is there any way to evaluate the expression with out using comparison operator.
Well your all conditions are looks fine. you must need to add php `error_reporting() in your code, this will help you to find syntax and warnings.
You have a typo here:
// missing $ sign
file_name = "albumpdf_" . $Order['products_name'] . "_" . $OrderId . "_" . $OrderPId . ".pdf";
And its better to use print_ready=0 instead of print_ready=false for $_GET.
Otherwise, you must need to check with === for value+dataType
$print_ready_flag = $_GET['print_ready'];
echo $print_ready_flag ; // I'm getting correct value
$print_ready_flag = filter_var($print_ready_flag, FILTER_VALIDATE_BOOLEAN);
if ($print_ready_flag == false) {
file_name = "albumpdf_" . $Order['products_name'] . "_" . $OrderId . "_" . $OrderPId . ".pdf";
}

php POST data to datetime

I have an issue with my project. I want to create a .ics file with start and endtime defined by the user of the website. So I get a string for the start and the end year and for the start/end time as well. Now I want to create a DateTime object from this data. My problem is that I just get the timestamp from right now and not from the string made:
$eventStartDay = $_POST["eventStartDay"];
$eventStartMonth = $_POST["eventStartMonth"];
$eventStartYear = $_POST["eventStartYear"];
$eventStartHour = $_POST["eventStartHour"];
$eventStartMinute = $_POST["eventStartMinute"];
$eventStartSecond = $_POST["eventStartSecond"];
$startDateTime = $eventStartYear . "-" . $eventStartMonth . "-"
. $eventStartDay . "T" . $eventStartHour . ":"
. $eventStartMinute . ":" . $eventStartSecond . "+01:00";
$eventEndDay = $_POST["eventEndDay"];
$eventEndMonth = $_POST["eventEndMonth"];
$eventEndYear = $_POST["eventEndYear"];
$eventEndHour = $_POST["eventEndHour"];
$eventEndMinute = $_POST["eventEndMinute"];
$eventEndSecond = $_POST["eventStartSecond"];
$endDateTime = $eventEndYear . "-" . $eventEndMonth . "-"
. $eventEndDay . "T" . $eventEndHour . ":"
. $eventEndMinute . ":" . $eventEndSecond . "+01:00";
$cal = new SimpleICS();
$cal->addEvent(function($e) {
$e->startDate = new DateTime($startDateTime);
$e->endDate = new DateTime($endDateTime);
I checked the strings with var_dump() and I got the right answers. They should look like this: '2016-01-18T19:00:00+01:00'
Quoting the PHP manual on Anonymous functions:
Closures may also inherit variables from the parent scope. Any such variables must be passed to the use language construct.
So you need to close over the DateTime objects, e.g. you need to do
$cal->addEvent(function($e) use ($startDateTime, $endDateTime) {
$e->startDate = new DateTime($startDateTime);
$e->endDate = new DateTime($endDateTime);
}

PDFLib (PCos) get image position from PDF

So i'm trying to get image position in PDF with PHP and PDFlib pCos library.
I can list images and get image dimensions like:
$imgcount = $tet->pcos_get_number($doc, "length:pages[" . $page . "]/images");
for ($im = 0; $im < $imgcount; $im++) {
$width = $tet->pcos_get_number($doc, "pages[" . $page . "]/images[" . $im . "]/Width");
$height = $tet->pcos_get_number($doc, "pages[" . $page . "]/images[" . $im . "]/Height");
}
But how do i get image position?
Already tried paths like:
"pages[" . $page . "]/images[" . $im . "]/Box"
"pages[" . $page . "]/images[" . $im . "]/Box[0]"
"pages[" . $page . "]/images[" . $im . "]/Rect"
Nothing works.. and i have searched in web everywhere,
checked also the PDF reference manual.. nothing there..
any suggestions?
To determine the position of an image you have to parse the page content. This is not possible with pCOS. For this, PDFlib offers an additional tool named TET:
http://www.pdflib.com/products/tet/
Please see TET manual, chapter 8.5 "Geometry of Placed Images" for further details on this topic.
But from your code fragment it looks like you already use the pCOS interface within TET. If so, simply check the related manual chapter.
A good starting point might be the TET 4.3 sample image_extractor.php, where you should simply add the positions to the following code fragment:
while ($ti = $tet->get_image_info($page) ) {
/* Report image geometry */
print("page " . $pageno . ": " .
(int) $ti->width . "x" .
(int) $ti->height . "pt [" .
(int) $ti->x . "x" .
(int) $ti->y . "], alpha=" .
$ti->alpha . ", beta=" .
$ti->beta . "\n");

Creating a directory in PHP based on a bunch of variables

I've been trying to create a directory following a specific structure, yet nothing appears to be happening. I've approached this by defining multiple variables as follows:
$rid = '/appicons/';
$sid = '$artistid';
$ssid = '$appid';
$s = '/';
and the function I've been using runs thusly:
$directory = $appid;
if (!is_dir ($directory))
{
mkdir($directory);
}
That works. However, I want to have the following structure in created directories: /appicons/$artistid/$appid/
yet nothing really seems to work. I understand that if I were to add more variables to $directory then I'd have to use quotes around them and concatenate them (which gets confusing).
Does anyone have any solutions?
$directory = "/appicons/$artistid/$appid/";
if (!is_dir ($directory))
{
//file mode
$mode = 0777;
//the third parameter set to true allows the creation of
//nested directories specified in the pathname.
mkdir($directory, $mode, true);
}
This should do what you want:
$rid = '/appicons/';
$sid = $artistid;
$ssid = $appid;
$s = '/';
$directory = $rid . $artistid . '/' . $appid . $s;
if (!is_dir ($directory)) {
mkdir($directory);
}
The reason your current code doesn't work is due to the fact you're trying to use a variable inside a string literal. A string literal in PHP is a string enclosed in single quotes ('). Every character in this string is treated as just a character, so any variables will just be parsed as text. Unquoting the variables so your declarations look like the following fixes your issue:
$rid = '/appicons/';
$sid = $artistid;
$ssid = $appid;
$s = '/';
This next line concatenates (joins) your variables together into a path:
$directory = $rid . $artistid . '/' . $appid . $s;
Concatenation works like this
$directory = $rid.$artistid."/".$appid."/"
When you're assigning one variable to another, you don't need the quotes around it, so the following should be what you're looking for.
$rid = 'appicons';
$sid = $artistid;
$ssid = $appid;
and then...
$dir = '/' . $rid . '/' . $sid . '/' . $ssid . '/';
if (!is_dir($dir)) {
mkdir($dir);
}

Categories