json_encode formatting - froala - php

I'm using Froalo for text editing, but I'm having difficulties getting the image upload to function correctly. Testing on localhost.
The documentation says that
imageUploadURL: '/upload_image.php',
should return a json string formated like this :
{ link: 'path/to/image.jpg' }
my javascript looks this this :
$(function() {
$('#edit').froalaEditor({
language:'fr',
imageUploadURL: 'upload.php'
})
});
my upload.php looks like this :
var_dump($_FILES);
require('clean.php'); // removes french special characters
$image = clean($_FILES['file']['name']);
$uploaddir = '../photos/';
$uploadfile = $uploaddir . basename($image);
$retour = ['link'=> $uploadfile];
$b = json_encode($retour);
$array_final = preg_replace('/"([a-zA-Z]+[a-zA-Z0-9_]*)":/','$1:',$b);
if( move_uploaded_file($_FILES['file']['tmp_name'],$uploadfile)) {
echo stripslashes($array_final);
} else {
echo "Lo kol kakh tov...";
}
When I run this from the text editor through froalaEditor,
the file gets uploaded to the server,
firebug says that upload.php
answers the array $_FILES and :
{link:"../photos/sama1.jpg"}
That all seems good, but froala answers that "something" went wrong and the images doesn't appear in the editor.
Could it be because of the double quotes around the image url?

Solution was dead simple : the problem was this:
{link:"../photos/sama1.jpg"}
It didn't like the relative path so changing it to either this :
{link:"/var/www/html/blabla/photos/sama1.jpg"}
or this
{link:"/photos/sama1.jpg"}
did the trick :)

Related

How to call an image alt by an onclick event using php?

I want to open a new page and transfer data from an array. Using the name of the image seemed like the easiest way so that's what i want to do.
on the page i want to call it
function meghiv($img)
{
$be=$img.alt;
echo $be;
session_start();
$_SESSION['kod'] = $be;
}
for($j=0;$j<4;$j++)
{
echo ' <img src="'.$nevek[$i].'.png" class="card-img-top " alt="'.$i.'" onclick="meghiv(this)"> ';
$i++;
}
on the new page
<?php
session_start();
echo $_SESSION['kod'];
?>
I don't know if it answers your question but try using javascript to load the image name into your php file
let images = document.querySelectorAll('.card-img-top'); // returns NodeList
let img_list = [...images]; // converts NodeList to Array
img_list.forEach(div => {
div.addEventListener("click", function(e){
e.preventDefault()
let alt = div.getAttribute("alt")
window.open(`https://link.here/?alt=${alt}`, "_blank");
})
});
Then in your php file
$image_name = $_GET['alt'];

Downloaded json file has extra slashes

I have found a way to download my json file that contains some json content in wordpress, on form submit, by calling on an external file download.php that will then execute header(). This works, however the json file that I get by downloading has escaped all characters. Even double quotes. And the json I echo out before downloading, doesn't.
Downloaded .json:
{\"post\":[{\"ID\":3467,\"post_author\":
Echoed out .json:
{"post":[{"ID":3467,"post_author":"1"
I've added the menu page:
add_menu_page( 'Download JSON', 'Download JSON', 'manage_options', 'custompage', 'download_json', 'dashicons-download', 6000 );
In my download_json() function, I have
$json_out = json_encode($output);
$download = htmlspecialchars($json_out);
echo '<form method="post" action="'.plugins_url().'/my_plugin/download.php">
<input type="hidden" name="json" value="'.$download.'">
<button type="submit" class="button-secondary">Download JSON</button>
</form>';
$output is my array with stuff in it. The json in the hidden input field looks like the echoed out one. In my download.php file I have
<?php
header("Content-type: application/force-download");
header('Content-Disposition: inline; filename="content.json"');
$json_contents = $_POST['json'];
echo $json_contents;
This downloads the content.json file when I click on the button, but I have extra escaped characters, and I'm not sure why.
Any help is appreciated.
Here is my old function I used on PHP4 to remove slashes, but only if they were automatically added.
Please note that usage of magic_quotes is highly discouraged. (I don't think it even exists in PHP5, not sure)
This function can take an plain value, or an array, and will do it on all elements then. I haven't used it in years, so be sure to test if it does what you want.
function stripslashesIfAutoAdded($something){
// This function removes added slashes
// It only removes them if they were added.
$mq_on = get_magic_quotes_gpc();
if (is_array($something)){
// loop over it and remove slashes where needed
$retArr = array();
foreach($something as $oneElement) {
if ($mq_on){
$retArr[] = stripslashes($oneElement);
} else {
$retArr[] = $oneElement;
}
}
return $retArr;
} else {
if ($mq_on){
return stripslashes($something);
} else {
return $something;
}
}
}

Pretty Photo load image via ajax frame

I have an angular app that will display some images. I am opening a prettyPhoto ajax window and passing the pathname to the URL. My script is loading the image fine, however, it isn't displaying the image how prettyPhoto traditionally displays a photo.
What do I need to do so it behaves like it is displaying a photo? i.e: has the fullscreen button, resizes the lightbox to the photo etc.
Is that even possible?
I am opening the lightbox via:
$scope.openLightbox = function()
{
if (typeof $.prettyPhoto.open !== "function")
{
$.fn.prettyPhoto({
social_tools:false,
deeplinking: false,
keyboard_shortcuts: false
});
$.prettyPhoto.open('resources/php/view/lightbox.php?ajax=true&path=' + $base64.encode($scope.currentFile));
return;
}
$.prettyPhoto.open('resources/php/view/lightbox.php?ajax=true&path=' + $base64.encode($scope.currentFile));
}
$scope.currentFile would be something like: data/39/my_image_name.jpg
and I am parsing the PHP like so:
$path = base64_decode($_GET['path']);
$finfo = finfo_open(FILEINFO_MIME_TYPE);
$mime = finfo_file($finfo, $path);
$mimeExt = explode('/', $mime);
if ($mimeExt[0] == 'image')
{
echo '<img width="100%" height="100%" src="data:image/' . $mimeExt[1] . ';base64,' . base64_encode(file_get_contents($path)) . '">';
}
elseif ($mimeExt[0] == 'video')
{
}
finfo_close($finfo);
Like I said above, the image is displaying just fine, I just want it to be displayed with the standard prettyPhoto image behavior. I understand this may not be possible.
EDIT
So turns out I didn't need AJAX afterall:
$scope.openLightbox = function()
{
if (typeof $.prettyPhoto.open !== "function")
{
$.fn.prettyPhoto({
social_tools:false,
deeplinking: false,
keyboard_shortcuts: false
});
$.prettyPhoto.open('resources/php/view/lightbox.php?path=' + $base64.encode($scope.currentFile));
return;
}
$.prettyPhoto.open('resources/php/view/lightbox.php?path=' + $base64.encode($scope.currentFile));
}
and finally my php which I am outputting the image directly to the browser so prettyPhoto thinks it is just loading an image
<?php
require("../config.php");
require("../connect.php");
session_start();
if (isset($_SESSION['ds_level']))
{
$path = base64_decode($_GET['path']);
$finfo = finfo_open(FILEINFO_MIME_TYPE);
$mime = finfo_file($finfo, $path);
$mimeExt = explode('/', $mime);
if ($mimeExt[0] == 'image')
{
header('Content-Type: image/' . $mimeExt[1]);
echo file_get_contents($path);
}
elseif ($mimeExt[0] == 'video')
{
//do other stuff to display video
}
finfo_close($finfo);
}
else
{
//-- no access
}
?>
What do I need to do so it behaves like it is displaying a photo? i.e: has the fullscreen button, resizes the lightbox to the photo etc.
Is that even possible?
Yes, it is possible. You need to create a dedicated directive, as specified in this Gist:
.directive('prettyp', function(){
return function(scope, element, attrs) {
$("[rel^='prettyPhoto']").prettyPhoto({deeplinking: false, social_tools: false});
}
})
To apply it, specify rel="prettyPhoto" in the anchor, like so:
<a prettyp ng-href="{{image.url}}" rel="prettyPhoto[main]" target="_blank" title="{{image.title}}">
HOW IT WORKS
The directive looks for a rel attribute starting with prettyPhoto, and applies the prettyPhoto magic to it.
EXAMPLE
I made a Plunk you can play around with: check the Plunk
IN YOUR CODE
To apply the directive in your code, you could replace:
echo '';
with:
echo '<img width="100%" height="100%" src="data:image/' . $mimeExt[1] . ';base64,' . base64_encode(file_get_contents($path)) . '">';
EDIT AFTER CHAT SESSION
As your images are protected with .htaccess, you have opted to work with a base64 version of your image, and why not?
However, it seems that if you wait until the user clicks the 'view' button in your app, it takes too long to go fetch the protected image and encode it, before passing it on to prettyPhoto:
I recommend you go fetch your image before the user clicks the view button, when the user selects the image in the list.
The long process of:
make an ajax call to php server;
have php app fetch image;
have php app encode image;
have angularjs/javaScript app store the base64 string
can then be done automatically, preventively, in the background.
The user experience would then be optimised.
So when the user does click the view button, the base64 version of the image can be passed to prettyPhoto straight away, without any server call: this would produce the same result as displayed in the plunkr I provided, when the button is pressed.

PHP Image not showing in HTML using img element

Hello there i have a php file with the included:
The image shows properly when i access the PHP file, however when I try to show it in the HTML template, it shows as the little img with a crack in it, so basically saying "image not found"
<img src="http://konvictgaming.com/status.php?channel=blindsniper47">
is what i'm using to display it in the HTML template, however it just doesn't seem to want to show, I've tried searching with next to no results for my specific issue, although I'm certain I've probably searched the wrong title
adding code from the OP below
$clientId = ''; // Register your application and get a client ID at http://www.twitch.tv/settings?section=applications
$online = 'online.png'; // Set online image here
$offline = 'offline.png'; // Set offline image here
$json_array = json_decode(file_get_contents('https://api.twitch.tv/kraken/streams/'.strtolower($channelName).'?client_id='.$clientId), true);
if ($json_array['stream'] != NULL) {
$channelTitle = $json_array['stream']['channel']['display_name'];
$streamTitle = $json_array['stream']['channel']['status'];
$currentGame = $json_array['stream']['channel']['game'];
echo "<img src='$online' />";
} else {
echo "<img src='$offline' />";
}
The url is not an image, it is a webpage with the following content
<img src='offline.png' alt='Offline' />
Webpages cannot be displayed as images. You will need to edit the page to only transmit the actual image, with the correct http-headers.
You can probably find some help on this by googling for "php dynamic image".
Specify in the HTTP header that it's a PNG (or whatever) image!
(By default they are interpreted as text/html)
in your status.php file, where you output the markup of <img src=... change it to read as follows
$image = file_get_contents("offline.png");
header("Content-Type: image/png");
echo $image;
Which will send an actual image for the request instead of sending markup. markup is not valid src for an img tag.
UPDATE your code modified below.
$clientId = ''; // Register your application and get a client ID at http://www.twitch.tv/settings?section=applications
$online = 'online.png'; // Set online image here
$offline = 'offline.png'; // Set offline image here
$json_array = json_decode(file_get_contents('https://api.twitch.tv/kraken/streams/'.strtolower($channelName).'?client_id='.$clientId), true);
header("Content-Type: image/png");
$image = null;
if ($json_array['stream'] != NULL) {
$channelTitle = $json_array['stream']['channel']['display_name'];
$streamTitle = $json_array['stream']['channel']['status'];
$currentGame = $json_array['stream']['channel']['game'];
$image = file_get_contents($online);
} else {
$image = file_get_contents($offline);
}
echo $image;
I suppose you change the picture dynmaclly on this page.
Easiest way with least changes will just be using an iframe:
<iframe src="http://konvictgaming.com/status.php?channel=blindsniper47"> </iframe>

Error when get url name of images?

I have a code download image from link http://www.bitrepository.com/download-image.html
When start is link format: <img src='test[1].jpg'>
But when download this link is link become <img src='test3%5B1%5D.jpg'>
How to fix it?
code here
<?php
include_once 'class.get.image.php';
// initialize the class
$image = new GetImage;
$image->source = 'http://test.com/test[1].jpg';
$image->save_to = 'images/'; // with trailing slash at the end
$get = $image->download('gd'); // using GD
if($get)
{
echo 'The image has been saved.';
}
?>
Try this.
On this line
$image->source = 'http://test.com/test[1].jpg';
Changed to
$image->source = htmlspecialchars_decode('http://test.com/test[1].jpg');
Look up urldecode in php to change the encoded values back to brackets

Categories