Converting from server image URL to absolute address - php

I guess this is a simple question but I really can't find anything that would help me.
I'm using an image download script and for that I need an image URL that looks like:
"/home/clients/websites/w_apo/public_html/apo/wp-content/uploads/2012/05/Beautiful.jpg"
[type1]
but what I have is:
"http://www.apo.com/wp-content/uploads/2012/05/Beautiful.jpg"
[type2]
I tried to write a function that returns the URL of type 1 to the URL of type 2 but it doesn't work all that great.
This is how it looks:
function relativeToServerAddress($relativeURL) {
$paths = explode('wp-content/', $relativeURL);
$thePath = getcwd()."/wp-content"."/".$paths[1];
return $thePath;
}
Is there any better way to do this? I tried to find a predefined function but failed.
Thanks

$file = basename("http://www.apo.com/wp-content/uploads/2012/05/Beautiful.jpg");
return getcwd()."/wp-content/".$file;
?
you need to inform us with the directories you wish to keep/move from absolute url;
the above just copies the file.

Related

How to output an image to pdf in table specific php code?

I am being asked to only add code to the following function. I want to output a logo and additional text if possible. And the content of the export document is $this->ExportDoc->Text. I am using the following but it's not working.
The original example is
function Page_Exporting() {
$this->ExportDoc->Text = "my header";
return TRUE;
}
I tried the following and they wouldn't work
function Page_Exporting() {
$this->ExportDoc->Text('logo.jpg');
return TRUE;
}
or
I tried to add additional lines, the second always replace the previous, they refuse to show at the same time....
function Page_Exporting() {
$this->ExportDoc->Text = "my header1";
$this->ExportDoc->Text = "my header2";
return TRUE;
}
I just want to add logo.jpg and couple lines to the top of the pdf I want to export from php code.
With this lib it seems easier !
https://github.com/PHPOffice/PHPWord
Example of the official documentation :
https://phpword.readthedocs.io/en/latest/elements.html#images
PHPWord can generate pdf !
In other hand, are you sure about your path ?
$this->ExportDoc->Text('logo.jpg');
logo.jpg is on the same path as your php script ?
For example to get the current path of your script use __DIR__.
https://secure.php.net/manual/en/language.constants.predefined.php
And finaly had the absolute path use realpath.
https://www.php.net/manual/en/function.realpath.php
Example of code :
$path = realpath(__DIR__ . '/../Resources/Asset/images/logo.png');
ExportDoc isn't a php native capability, so what are you using ?

Saving image file name exactly as URL image file into folder in PHP

I have visited this article previously and found it useful, but i would like to add more functionality to it by having it save an image file name according to the URL name.
This is what I've done so far and it works.
$contents=file_get_contents('http://www.domain.com/logo.png');
$save_path="C:/xampp/htdocs/proj1/download/[logo.png]";
file_put_contents($save_path,$contents);
Basically, where I have put square brackets around I want to have that dynamic based on the URL file name. For example, if i have an image url such as this: https://cf.dropboxstatic.com/static/images/brand/glyph-vflK-Wlfk.png, I would like it to save the image into the directory with that exact image name which in this case is glyph-vflK-Wlfk.png.
Is this possible to do?
I would do this way
$url = "http://www.domain.com/logo.png";
$file = file_get_contents($url);
$path = "C:/xampp/htdocs/proj1/download/". basename($url);
return !file_exists($path) ? file_put_contents($path, $file) : false;
From what I understand, what you're trying to do is the following :
$url = 'http://www.domain.com/logo.png';
$contents=file_get_contents($url);
$posSlash = strrpos($url,'/')+1);
$fileName = substr($url,$posSlash);
$save_path="C:/xampp/htdocs/proj1/download/".$fileName;
file_put_contents($save_path,$contents);
There is a function for that, basename():
$url="http://www.domain.com/logo.png";
$contents=file_get_contents($url);
$save_path="C:/xampp/htdocs/proj1/download/".basename($url);
file_put_contents($save_path,$contents);
You might want to check if it already exists with file_exists().

How do I find the filename of an image on a MediaWiki page using php?

How do I find the filename of an image on a MediaWiki site?
I don't want to put the filename in manually. I need PHP code which will fetch me the filename.
I can use $f = wfFindFile( '$filename' ); but HOW DO I GET $filename?
I've been looking at the FILE class but I can't figure out how to use File::getFilename(); I keep getting an error call to undefined method.
What am I doing wrong?
Explaining in more detail:
I would like to add the pin it button to my site so when you click on the button it post it on the pin it board with the image and description of the image. I need to use php to send the image information so it works on every page on my site. I can't code the image name manually each time.
So far I have the code:
<img border="0" src="//assets.pinterest.com/images/PinExt.png" title="Pin It" />
Which works great except I need to put in a value for $f (image name). My question is how do I get the value of $f without having to put in in eg $f = wfFindFile( 'Sunset.jpg' );
I would have thought this would be a really common request for anyone trying to add pinterest to their site.
Thanks
The $filename you are looking for is basically how it is named in MediaWiki when it got uploaded, for example Landscape-plain.jpg. You will just use the wfFindFile() helper function to get a File object. Then call the methods:
$ php maintenance/eval.php
> $file = wfFindFile( 'Landscape-plain.jpg' );
> print $file->getName();
Landscape-plain.jpg
> print $file->getPath();
mwstore://local-backend/local-public/b/b0/Landscape-plain.jpg
> print $file->getFullPath();
/path/to/images/b/b0/Landscape-plain.jpg
> print $file->getTitle();
File:Landscape-plain.jpg
> exit
API documentation:
http://svn.wikimedia.org/doc/classFile.html
http://svn.wikimedia.org/doc/classLocalFile.html
EDIT BELOW
The file informations are available through a File object, so you definitely need to use wfFindFile() to get such an object.
To actually find the filename for the page the user is browsing on, you want to use the query context and get its title:
$context = RequestContext::getMain();
$t = $context->getTitle();
if( $title->getNamespace == 'NS_FILE' ) {
$filename = $title->getPrefixedText;
// do your stuff.
}

exploding a string with variable

I'm trying to manipulate a path to an image using php.
The path has a variable userdirectory in it, so i'm struggling with how to write this.
Ive used to do :
$texthtml = $content;
if (preg_match('/<img.+src=[\'"](?P<src>.+?)[\'"].*>/i', $texthtml, $image) ) {
$promopic = $image['src'];
}
to find if there is an image in my content and make a variable out of it. that works, but i need to alter my code to load image from a thumbnail directory for pageload reasons.
an image src looks like this : "/uploads/userdirs/admin(variable)/image.jpg"
But i want it to be this :
"/uploads/userdirs/admin(variable)/mcith/mcith_image.jpg"
adding a /mcith/ and a mcith_prefix to the image.
I'm thinking exploding it, but i dont know how to do that with a variable path. Any pointers greatly appreciated!
You can do this many different ways, but I would probably use pathinfo() for this:
$path = pathinfo($promopic);
$thumb = $path['dirname'] . '/mcith/mcith_' . $path['basename'];
very simple version, could use improvement
preg_replace('/\/([a-zA-Z0-9]+\.[a-zA-Z0-9]{3,4})$/', '/mcith/mcith_$1', '/uploads/userdirs/admin(variable)/image.jpg');

Saving Images to folder | PHP

I want to be able to open the provided URL (which is done via a form) that is an URL that will allow the server to save the file into a directory, for example:
http://www.google.co.uk/intl/en_com/images/srpr/logo1w.png
I want to save that logo into this directory:
img/logos/
Then it will add it to the database by giving it a random file name before so, e.g.
827489734.png
It will now be inserted to the database with the following:
img/logos/827489734.png
I do not want to use cURL for this, I like to work with fopen, file_get_contents, etc...
Cheers.
EDIT
$logo = safeInput($_POST['logo']);
if(filter_var($avatar, FILTER_VALIDATE_URL))
{
$get_logo = file_get_contents($logo);
$logo_directory = 'img/logos/';
$save_logo = file_put_contents($logo_directory, $logo);
if($save_logo)
{
$logo_path = $logo_directory . $save_logo;
A part of this code I need helping...
You need to specify a full file name when doing a file_put_contents(). A pure directory name won't cut it.

Categories