How to Fix "not allowed to load local resource" [Wordpress] - php

I am using the img tag but the code is in a php block.
<img class='client_image' src='" . get_stylesheet_directory() . "/assets/images/" . $print->client_images . "' alt='clientimage" . $clientindex . "' width='200' height ='200' />";
I keep getting not allowed to load local resource and I do not know why. I am using a premade wordpress theme. All I get is the broken image icon. The path is correct as well as the name of the file.

get_stylesheet_directory() will returns the path on your server, you need get_stylesheet_directory_uri() instead:
<img class='client_image' src='" . get_stylesheet_directory_uri() .
"/assets/images/" . $print->client_images . "' alt='clientimage" .
$clientindex . "' width='200' height ='200' />";

Related

Searching for file in directory using glob, not returning results

I'm trying to make a small script for retrieving configuration files containing mapping instructions for a product import, using php glob. But I fail miserably.
What I have right now:
echo "Shop type: " . $this->shopType . '<br />';
echo "Shop version: " . $this->shopVersion . '<br />';
echo "Glob search: config/mappings/" . $this->shopType . "_" . $this->shopVersion . ".php";
foreach (glob("config/mappings/" . $this->shopType . "_" . $this->shopVersion . ".php") as $filename) {
echo "$filename size " . filesize($filename) . "\n";
}
exit;
$this->shopType represents the name of the shop from which the export file should be imported. E.g. ccvshop.
$this->shopVersion represents the version of the shop, to be able to write different import mappings for different versions of the shop types.
The value of shopType = ccvshop.
The value of shopVersion = 11.2.
Which would make the search string in the glob function:
config/mappings/ccvshop_11.2.php.
Unfortunately my result is empty, and I can't see what I'm doing wrong.
Any help would be appreciated.
I added an image representing my directory structure:
glob() returns an array of matching files. It looks like you already know the name of the file that you want to read. Try something like this:
$filename = "config/mappings/" . $this->shopType . "_" . $this->shopVersion . ".php";
if (file_exists($filename))
echo "$filename:" . filesize($filename);

Cropping, Re-sizing and Saving images from one folder to another folder in wordpress

I'm adding an upgrade for the WordPress plugin which I have developed ages ago. This plugin is just a product catalog, so just show products and their images. Products can have more than one images.
I was re-sizing images in the older version of the plugin by CSS, assigning them width and height. Which was working but images look stretched but users were happy. Now I have added a new feature in the plugin which is to crop and re-size from the uploaded image and save it with a different name, like a thumbnail.jpg.
The new feature is working remarkably for the new users who upload images, but the thing is about old users who upgraded to newer version.
The issue is old users already has products and images. When I try to get all the products and images via foreach loop, it works perfectly on 200 - 250 images but breaks on more than 250+ images - No Error :(
Many of my old users has 600+ images so I want a way to crop and re-size the existing images and saves them with a new name and save the file names in the DB.
I'm using wordpress's default wp_get_image_editor(); function.
Here's my query to get old products which has images:
$wpc_product_images_sql = "Select wpc_posts.*, wpc_meta.* From " . $wpdb->posts . " As wpc_posts Inner Join " . $wpdb->postmeta . " As wpc_meta On wpc_posts.ID = wpc_meta.post_id Where wpc_meta.meta_key = 'product_images' Order By wpc_posts.post_title;
And here's my foreach loops (I'm using two loops. First one is getting the products which has images and the second loop is for get images from each post, as I'm mentioned earlier in my question that products can have more than one images. So have to use two loops)
foreach ($wpc_images_qry as $wpc_prod_images) {
echo '<div class="wpc_image_body">'
. '<h3>' . $wpc_prod_images->post_title . '</h3>'
. '<div class="wpc_images">';
$wpc_post_id = $wpc_prod_images->ID;
$wpc_product_images = get_post_meta($wpc_post_id, 'product_images', true);
$big_img_name = array();
foreach ($wpc_product_images as $wpc_prod_img) {
/// For Big
$big_resize_img = wp_get_image_editor($wpc_prod_img['product_img']);
if (!is_wp_error($big_resize_img)) {
$product_big_img = $wpc_prod_img['product_img'];
$product_img_explode = explode('/', $product_big_img);
$product_img_name = end($product_img_explode);
$product_img_name_explode = explode('.', $product_img_name);
$product_img_name = $product_img_name_explode[0];
$product_img_ext = $product_img_name_explode[1];
$big_crop = array('center', 'center');
$big_resize_img->resize($wpc_image_width, $wpc_image_height, $big_crop);
$big_filename = $big_resize_img->generate_filename('big-' . $wpc_image_width . 'x' . $wpc_image_height, $upload_dir['path'], NULL);
$big_resize_img->save($big_filename);
$big_img_name[]['wpc_big_img'] = $upload_dir['url'] . '/' . $product_img_name . '-big-' . $wpc_image_width . 'x' . $wpc_image_height . '.' . $product_img_ext;
if (file_exists($upload_dir['path'] . '/' . $product_img_name . '-big-' . $wpc_image_width . 'x' . $wpc_image_height . '.' . $product_img_ext)) {
echo $upload_dir['path'] . '/' . $product_img_name . '-big-' . $wpc_image_width . 'x' . $wpc_image_height . '.' . $product_img_ext . ' - <strong style="color: #7ad03a;">OK</strong><br>';
} else {
echo $upload_dir['path'] . '/' . $product_img_name . '-big-' . $wpc_image_width . 'x' . $wpc_image_height . '.' . $product_img_ext . ' <strong style="color: red">:(</strong><br>';
}
}
}
update_post_meta($wpc_post_id, 'wpc_big_images', $big_img_name);
echo '</div>'
. '</div>';
}
You might try adding the new image size with
add_image_size()
and then run
https://wordpress.org/plugins/regenerate-thumbnails/
which surely has a solution to the timeout problem.
There is a way to display a message for your plugin users upon update, ask them to install and run the plugin if they have old images.
Hope that works for you.
If you want resize image mean follow these one, it resize your image whenever upload image in media,
add_filter('wp_handle_upload_prefilter','aj_handle_upload');
function aj_handle_upload($file)
{
add_image_size( 'mobile', 360, 0, array( 'center', 'center' ));//mobile is userdefined name.
add_image_size( 'desktop', 700, 0, array( 'center', 'center' ));//desktop & tablet is user defined name.
return $file;
}

removing <br> from image hover over caption in php

I am working on showing images via a database for a project with slimbox formatting, and am having issues with the mouse-over/hover-over caption. In the code I have
echo '<a href="images/' . $filename . '" rel="lightbox-set1"
title="' . $description.'<br>'."licence: ". $Licence. $Owner.'">';
echo '<img src="images-thumb/' . $filename . '" />';
echo '</a>';
and the mouse over caption for the image on screen shows:
The Lagoon in wellington<br>licence:free for all
I want to know if there is a way to remove the <br> from the caption displayed, without actually removing it, (<br>), from the code itself.
Change the <br> tag to a PHP line break PHP_EOL:
title="' . $description . PHP_EOL . 'licence: ' . $Licence . $Owner . '">';
This will insert a carriage return inside your title.
echo '<a href="images/' . $filename . '" rel="lightbox-set1"
title="' . $description.'&#13;'."licence: ". $Licence. $Owner.'">';
echo '<img src="images-thumb/' . $filename . '" />';
echo '</a>';
If you plan to have a new line when the user hovers to your image try this one
echo '<a href="images/' . $filename . '" rel="lightbox-set1"
title="' . $description.' 
 '."licence: ". $Licence. $Owner.'">';

how to play/display video in a directory with html5

I do not know how to get videos in a folder to play in html or php. I know how to do a single video but that's not what I want. I want to display and play videos that are in a directory folder. Is their a certain way to do this. I tried this code but it doesn't work and I tried other codes. I know in the code below its connected to a database. I have managed to upload videos but can't display them.
none of the solutions given is working but let me ask a simpliar question can I do this to get not just one particular video but all the videos with a path to the folder with videos. I think the code above has a lot of errors and it was the only one i could find so I'm not trusting the code above.
Can you try the following?
<?php
$query = "select * from videos where `id`='" . $id . "'";
$select = mysql_query($query);
while (($row = mysql_fetch_assoc($select)) != null)
{
$video = $row['video'];
print '<video width="320" height="240" controls>' .
'<source src="uploadvideo/' . $video . '" type="video/mp4">' .
'<source src="video/' . $video . '" type="video/ogg" />' .
'Your browser does not support the video tag.' .
'</video>';
}
?>
There are a few tweaks in there, such as using backticks around the column name, inputting a message for if your browser does not support the video tag (taken from here), and handling a null result.
Also, maybe you need to stick a / in front of video/ and uploadvideo/? e.g. if your videos are located in one folder below the document root, such as wwww.mydomain.com/video/vid-name-here.mp4
Another thing, have you removed the file extensions from all the videos, or is the file extension kept in the database for both mp4 and ogg videos? Perhaps you want to attach .mp4 and .ogg accordingly in the source links to be like so:
'<source src="uploadvideo/' . $video . '.mp4" type="video/mp4">' .
'<source src="video/' . $video . '.ogg" type="video/ogg" />' .
<?php
$query = "select * from `videos` where `id`='" . $id . "'";
$result = mysql_query($query);
if(mysql_num_rows($result) > 0){
while ($row = mysql_fetch_array($result))
{
$video = $row['video'];
echo '<video width="320" height="240" controls>' .
'<source src="uploadvideo/' . $video . '" type="video/mp4">' .
'<source src="video/' . $video . '" type="video/ogg" />' .
'Your browser does not support the HTML5 tags.' .
'</video>';
}
}
?>

PHP Image prints out garbage

Attempting to print out an image to the browser using code I copied from http://php.net/manual/en/function.imagecopyresampled.php.
It prints out a box of random characters.
CODE:
public function printSummaryArticle($article, $copy, $thumb) {
$src_image = Config::getAbsPath() . '/images/articles/' . $article['image'];
echo
'<div class="summary_article"><a href="/'
. BreadCrumbs::getCrumb(1)
. '/'
. BreadCrumbs::getCrumb(2)
. '/article/'
. $article['id']
. '"><h4>'
. $article['title']
. '</h4></a> ('
. $article['date']
. ')'
. '<img src="data:image/jpeg;base64,'. imagejpeg($thumb->generateThumb($src_image, 300, 200)) . '"'
. '<p>'
. strip_tags($copy->truncateString($article['body'], 250, " "))
. '</p><p><a href="/' . BreadCrumbs::getCrumb(1)
. '/'
. BreadCrumbs::getCrumb(2)
. '/article/'
. $article['id']
. '"> Read more</a></p></div>';
}
Also tried:
public function printSummaryArticle($article, $copy, $thumb) {
$src_image = Config::getAbsPath() . '/images/articles/' . $article['image'];
echo
'<div class="summary_article"><a href="/'
. BreadCrumbs::getCrumb(1)
. '/'
. BreadCrumbs::getCrumb(2)
. '/article/'
. $article['id']
. '"><h4>'
. $article['title']
. '</h4></a> ('
. $article['date']
. ')';
header('Content-type: image/jpeg');
imagejpeg($thumb->generateThumb($src_image, 300, 200));
echo
'<p>'
. strip_tags($copy->truncateString($article['body'], 250, " "))
. '</p><p><a href="/' . BreadCrumbs::getCrumb(1)
. '/'
. BreadCrumbs::getCrumb(2)
. '/article/'
. $article['id']
. '"> Read more</a></p></div>';
}
Same result. except with an added error claiming headers have already been sent.
How can I fix this?
imagejpeg() neither returns a string nor performs Base64 encoding. To work around this, capture its output in a PHP output buffer, and then Base64 encode the captured output:
ob_start();
imagejpeg( $my_img );
echo '<img src="data:image/jpeg;base64,' . base64_encode(ob_get_clean()) . '">';
Note that data: URLs are limited to 32 KB in Internet Explorer 8 and do not work in earlier versions of IE (source). If you need to support IE 8 and below, you may want to instead save the image as a separate file on the server. This is left as an exercise for the reader :)
(For an explanation of the "Headers already sent" warning, see How to fix "Headers already sent" error in PHP.)
You haven't closed the image tag, but more importantly you haven't base64_encoded the image data
Before you echo that string
ob_start();
imagejpeg($thumb->generateThumb($src_image, 300, 200));
$imagejpg=ob_get_clean();
then this
. '<img src="data:image/jpeg;base64,'
. base64_encode($imagejpg)
. '" />'
You can't do what you're trying to do in the 2nd example

Categories