How can i convert a FilePath to a File in PHP/Laravel? - php

For example I have this file:
$thumbnail = 'http://www.example.com/pic.jpeg';
I try this:
$thumbnail = file_get_contents($thumbnail);
dd($thumbnail->getClientOriginalExtension());
I got this error:
"Call to a member function getClientOriginalExtension() on string"

Are you storing it on disk? This worked in my tester.
use Storage;
$url = "http://www.google.co.in/intl/en_com/images/srpr/logo1w.png";
$contents = file_get_contents($url); $name = substr($url, strrpos($url, '/') + 1);
Storage::put($name, $contents);

Related

Get values from url with PHP

I'm trying to get values from a url using php. With basename I only get the last part but i need the part before that as well.
This is the domain: http://mydomain.nl/first/second/third/
$url = parse_url($entry['source_url']);
$urlFragments = explode('/', $url);
$second = $urlFragments[0];
$third = $urlFragments[1];
I need to use part second and part third.
#idka-80 try this,
$url_components = parse_url($url);
echo "<pre>";
print_r(array_filter(explode("/",$url_components['path'])));
This script can help you
First of all, to make it simple I remove http:// part and then explode it with / and get a different part of the data which is separated by /
<?php
$url = "http://mydomain.nl/first/second/third/";
$url = str_replace("http://", "", $url);
$urlFragments = explode('/', $url);
$yourDomain = $urlFragments[0];
$first = $urlFragments[1];
$second = $urlFragments[2];
$third = $urlFragments[3];
echo $first . ", " . $second . ", " . $third;
As you can tell from the fatal error, you're making a wrong assumption about how parse_url() works:
Fatal error: Uncaught TypeError: explode(): Argument #2 ($string) must be of type string, array given
If you only want a specific fragment, you need to tell which one:
$url = parse_url($entry['source_url'], PHP_URL_PATH);
// ^ Also give it a better name, such as `$path`
You also possibly want to discard leading and trailing slashes:
$urlFragments = explode('/', trim($url, '/'));
Hopefully this will help
$url = 'http://mydomain.nl/first/second/third/';
$urlFragments = explode('/', $url);
echo $second = $urlFragments[4];
echo $third = $urlFragments[5];

Openssl_pkcs7_sign(): error opening file

it's my first time doing signing of cert using openssl. Keep hitting the above error and tried realpath() and appending file:// but still can't get openssl to sign the profile. I don't understand how this works. Any insights would be appreciated.
Edit1: I'm not sure which file is the problematic one. The error messages wasn't specific enough. Is there a way to tell?
Code and screenshots below:
function signProfile()
{
$filename = "./template.mobileconfig";
$filename = realpath($filename);
$outFilename = $filename . ".tmp";
$pkey = dirname(__FILE__) . "/PteKey.key";
$pkey = realpath($pkey);
$certFile = dirname(__FILE__) . "/CertToSign.crt";
$certFile = realpath($certFile);
// try signing the plain XML profile
if (openssl_pkcs7_sign($filename, $outFilename, 'file://'.$certFile, array('file://'.$pkey, ""), array(), 0, ""))
{
// get the data back from the filesystem
$signedString = file_get_contents($outFilename);
// trim the fat
$trimmedString = preg_replace('/(.+\n)+\n/', '', $signedString, 1);
// convert to binary (DER)
$decodedString = base64_decode($trimmedString);
// write the file back to the filesystem (using the filename originally given)
$fh = fopen($filename, 'w');
fwrite($fh, $decodedString);
fclose($fh);
// delete the temporary file
unlink($outFilename);
return TRUE;
}
else
{
return FALSE;
}
}
Remove unwanted fields if not used in
openssl_pkcs7_sign($mobileConfig, $tmpMobileConfig, $certFile, array($pkey, ""), array());
Make sure file paths are correctly supplied.
require_once('variables.php'); //stores abs path to $tmpMobileConfig/$pteKeyPath/$CertToSignPath
$prepend = "file://";
$mobileConfig = realpath("./template.mobileconfig");
$pkey = $prepend . $pteKeyPath;
$pkey = str_replace('\\', '/', $pkey);
$certFile = $prepend . $CertToSignPath;
$certFile = str_replace('\\', '/', $certFile);
$isSignedCert = openssl_pkcs7_sign($mobileConfig, $tmpMobileConfig, $certFile, array($pkey, ""), array());

Imagick - Can't read image files from URL.

I'm using this snippet for reading images on different websites:
$image = new Imagick('http://lp.hm.com/hmprod?set=key[source],value[/model/2012/P01 05156 06204 80 1175 4.jpg]&set=key[rotate],value[]&set=key[width],value[]&set=key[height],value[]&set=key[x],value[]&set=key[y],value[]&set=key[type],value[STILL_LIFE_FRONT]&call=url[file:/product/large]');
But sometimes, I get an error like this (about 20% of the time):
ImagickException
Unable to read the file: http://lp.hm.com/hmprod?set=key[source],value[/model/2012/P01 05156 06204 80 1175 4.jpg]&set=key[rotate],value[]&set=key[width],value[]&set=key[height],value[]&set=key[x],value[]&set=key[y],value[]&set=key[type],value[STILL_LIFE_FRONT]&call=url[file:/product/large]
Imagick->__construct()
The error seems to be consistent through this whole domain, but sometimes it's different from image to image on the same domain.
Questions
Why is this a problem?
How can we fix it?
Is there an alternative solution?
STEP 1 : GET URL
$url = 'http://blablabla.com/blabla.jpeg';
STEP 2 : Save blog content to a variable
$image = file_get_contents($url);
STEP 3 : Create Imagick object
$img = new Imagick();
STEP 4 : Instruct imagick object to read blob content of the image
$img -> readImageBlob($image);
STEP 5 : Save (or do operations) on image
$img -> writeImage('/var/www/html/uploads/img.jpg');
STEP 6 : Destroy imagick instance
$img -> destroy();
So I figured out I needed to encode the url properly. I'm not sure if this code is optimal, but it works.. and could hopefully help someone else.
$parsedUrl = parse_url('http://lp.hm.com/hmprod?set=key[source],value[/model/2012/P01 05156 06204 80 1175 4.jpg]&set=key[rotate],value[]&set=key[width],value[]&set=key[height],value[]&set=key[x],value[]&set=key[y],value[]&set=key[type],value[STILL_LIFE_FRONT]&call=url[file:/product/large]');
$info = pathinfo($parsedUrl['path']);
$dirname = explode('/', $info['dirname'] ?: '');
$dirname = array_filter($dirname, 'strlen');
$dirname = array_map('urlencode', $dirname);
$dirname = implode('/', $dirname);
$basename = urlencode($info['basename'] ?: '');
$path = array_filter(array($dirname, $basename), 'strlen');
$path = '/' . implode('/', $path);
$query = explode('&', $parsedUrl['query'] ?: '');
foreach ($query as &$set)
{
$set = explode('=', $set, 2);
$set = array_map('urlencode', $set);
$set = implode('=', $set);
}
$query = implode('&', $query);
$uri = array_filter(array($path, $query), 'strlen');
$uri = implode('?', $uri);
$fragment = urlencode($info['fragment'] ?: '');
$uri = array_filter(array($uri, $fragment), 'strlen');
$uri = implode('#', $uri);
$scheme = $parsedUrl['scheme'] ?: '';
$host = $parsedUrl['host'] ?: '';
$url = array_filter(array($scheme, $host), 'strlen');
$url = implode('://', $url);
$url .= $uri;
$image = new Imagick($url);
OBS!
This code will leave notifications.
Try to use urlencode function for encode special chars of url:
$image = new Imagick(urlencode('http://lp.hm.com/hmprod?set=key[source],value[/model/2012/P01 05156 06204 80 1175 4.jpg]&set=key[rotate],value[]&set=key[width],value[]&set=key[height],value[]&set=key[x],value[]&set=key[y],value[]&set=key[type],value[STILL_LIFE_FRONT]&call=url[file:/product/large]'));
Or if not work, try this:
$content = file_get_contents(urlencode('http://lp.hm.com/hmprod?set=key[source],value[/model/2012/P01 05156 06204 80 1175 4.jpg]&set=key[rotate],value[]&set=key[width],value[]&set=key[height],value[]&set=key[x],value[]&set=key[y],value[]&set=key[type],value[STILL_LIFE_FRONT]&call=url[file:/product/large]'));
$image = new Imagick($content);
I just encountered a similar issue. I was using file_get_contents() and was getting the same "ImagickException The filename is too long". The fix for me was finding the tmp directory and setting the correct permissions for it.

Do multiple search and replaces in PHP string?

I am working with PHP and WordPress right now, I need to basically run the below code to Replace text in $current_path with the text in $new_path if $current_path EXIST in $content
I would prefer to be able to iterate over an array instead of running this over and over like this, or any better method would be nice?
$content = 'www.domain.com/news-tag/newstaghere'
$current_path = 'test-tag';
$new_path = 'test/tag';
$content = str_replace($current_path, $new_path, $content);
$current_path = 'news-tag';
$new_path = 'news/tag';
$content = str_replace($current_path, $new_path, $content);
$current_path = 'ppc-tag';
$new_path = 'ppc/tag';
$content = str_replace($current_path, $new_path, $content);
str_replace() accepts array arguments:
$current_paths = array('test-tag','news-tag','ppc-tag');
$new_paths = array('test/tag','news/tag','ppc/tag');
$new_content = str_replace($current_paths, $new_paths, $content);
Or you can use a single array with strtr():
$path_map = array('test-tag'=>'test/tag', 'news-tag'=>'news/tag', 'ppc-tag'=>'ppc/tag');
$new_content = strtr($content, $path_map);
However, you seem to be doing something very generic. Maybe all you need is a regex?
$new_content = preg_replace('/(test|news|ppc)-(tag)/u', '\1/\2', $content);
Or maybe even just
$new_content = preg_replace('/(\w+)-(tag)/u', '\1/\2', $content);
$content = 'www.domain.com/news-tag/newstaghere'
$current_paths = array('test-tag','news-tag','ppc-tag');
$new_paths = array('test/tag','news/tag','ppc/tag';
$content = str_replace($current_paths, $new_paths, $content);
Array arguments can be provided for the str_replace function, as noted on the following PHP.net page:
http://php.net/manual/en/function.str-replace.php
Please see "Example #2" on the page linked above for details.
You can do that:
$content = 'www.domain.com/news-tag/newstaghere';
$content = preg_replace('~www\.domain\.com/\w++\K-(?=tag/)~', '/', $content);

How to get user's profile pic url using graph api

Hi i m trying to get url of user's profile pic so I use following code
$userinfo = $facebook->api("/me/picture?type=large");
var_dump($userinfo);
but instead of returning url it returns NULL. Please help me why this is happening.
Thank you.
This is how we can get actual picture url
$URL='FB GRAPH API URL';
$headers = get_headers($URL, 1); // make link request and wait for redirection
if(isset($headers['Location'])) {
$URL = $headers['Location']; // this gets the new url
}
$url_arr = explode ('/',$URL);
$ct = count($url_arr);
$name = $url_arr[$ct-1];
$name_div = explode('.', $name);
$ct_dot = count($name_div);
$img_type = $name_div[$ct_dot -1];
$pos = strrpos($img_type, "&");
if($pos)
{
$pieces = explode("&", $img_type);
$img_type = $pieces[0];
}
$imagename = imgnameyouwant'.'.$img_type;
$content = file_get_contents($URL);
file_put_contents("fbscrapedimages/$imagename", $content);
If you know the [uid], you can simply use the following code
<img src="http://graph.facebook.com/[UID]/picture" />
You can also crop/resize the profile picture by providing height/width parameters.
Eg: https://graph.facebook.com/[UID]/picture?width=140&height=140
If you want to use standard sizes, try
<img src="http://graph.facebook.com/[UID]/picture?type=large" />
where type is one of {square,small,large}

Categories