I am working in a CakePHP application, where I am using HTML helper's image() function to show images. What I want, if it gets the image in the given path, then it will show the image, otherwise, it'll show a default image. I have that "default" image, but I don't understand where/how to define it.
Thanks
Extend the HTML helper and add the logic to the image method and override the called image with your default image if the originally requested image does not exist.
index.ctp file code
<?php
$MyImage = $post['Post']['image0']; // image0 is my db field
if (file_exists("img/uploads/posts/".$MyImage))
$MyImage1 ="/img/uploads/posts/".$MyImage;
else
$MyImage1 = "/img/uploads/posts/no-image.jpg";
echo $this->Html->image($MyImage1);
?>
Note : My post's images are stored in app/webroot/img/posts folder(default image is also in this folder.)
Related
Is there a way I can save unisharp standalone image file name in my model and then get in edit form?
I want to make featured image for my articles model. I want to get that featured image in unisharp filemanager standalone button when I click and get select previous uploaded image i want to save that image name in my model articles.
And how can I call that image path in my edit form articleController.
I want something like Wordpress. Select featured image if previous uploaded or upload new one and select it.
Something like that. I think you get the point.
You should have a field for the image in your table and its model, the field should be in $fillable array
according to unisharp docs the stand alone button should be like this filling the proper input right ?
<button type="button" id="lfm-image" data-input="thumbnail" data-preview="holder">Choose Image</button>
<img id="holder"src="{{isset($model->image) ? :''}}">
<input type="hidden" id="thumbnail" name="filepath">
Then you get it in the controller like this
if ($request->filepath != null) {
$page->image = $request->filepath;
$page->save();
}
It works like charm in edit form as well
I need to display an image using a URL that I receive from Push Notification. I tried many docs and blogs but couldn't find the correct solution. I don't want to download and store in DB.
the easiest way that i found to do this...
map the url to an image box and make it visible, set an events event to success, set action to Set property, set component name to what you want to show the image, set property to visibile and value to true leave check box empty as in this image success to html function
I my thumbnails are not showing properly for multiple image view. On the admin side I can see all the different image set for a single product. I selected the default main, base and thumbnail image but on the front end I only see the default image. The other images dont show instead the default image is repeated.
Ok nevermind I have found the problem. An extension was causing the conflict. I just simply unistalled the extension and everything started working Ok.
The culprit extension is called, 'Image Permalink' or 'IG_NamedImages' in the magento connect.
Please let me know if anyone was having the same problem
I fixed this module editing the IG_NamedImages_Model_Product_Image as the following:
class IG_NamedImages_Model_Product_Image extends Mage_Catalog_Model_Product_Image
{
public function setBaseFile($file)
{
parent::setBaseFile($file);
if (Mage::helper('ig_namedimages')->getIsEnabled()){
$product = $this->getProduct();
$imageName = Mage::getSingleton('catalog/product_url')->formatUrlKey($product->getName());
$this->_newFile = preg_replace('/\/([^\/]+\.\w+)$/', '/'.$imageName.'-\1', $this->_newFile);
}
return $this;
}
}
I have create private folder in application folder: application/private/username, I'm uploading images here with ajax, that works, but how can I retrieve that image with ajax, and display it in img tag. First problem is that http://example.com/application/private/useraname/img.jpg is protected, I can't access it through out url in browser, how to display an image. Just advice, I wanna write code on my own.
This is part of function(that is part of controller) that grabs image:
$filepath = 'path to image';
header("Content-type: image/jpeg");
if (file_exists($filepath)) {
$img_handle = imagecreatefromjpeg($filepath) or die("");
echo $img_handle;
ImageJpeg($img_handle);
}
But how to get this image with ajax?
You might want to create a PHP file that would get the filename eg thumb.php?src=img.jpg and inside that file, you load the image from that path and display it. This way the path of the images are not exposed to the user.
I will try to be as clear as possible to explain my problem. If not, tell me !
Mywebapp is based on Codeigniter. In it, I did a controller in order to show images uploaded by the users. By this way, the images can only be seen by members or authorized people and are not public like in facebook for example (image addresses are just hashed but are public).
I also use the mpdf library to generate a pdf from the content of a web page. To use it, I have in the third party directory the mpdf library. To be able to use it with my formatting, I put in the library directory (in application directory) my mpdf library (just to be able to use it from different controller). I have a pdf controller which is called from the view where I want to generate the pdf.
So the process is : I call a function of the pdf controller from my view. In this function, I call a function of my mpdf library which create a mpdf object (the third party class) and generate the pdf.
The generation of the pdf works great. The only I had is for the images. I explain. In my html document, the image are embedded like that :
<img src="http:localhost/www/myapp/library/image/hashed_name_of_the_image" alt="" />
The image function of the library controller retrieve the image and shows it. But in this function, I can protect or not the access to run it. For example, to force to be a logged user to see an image (if($this->session->userdata(‘logged’) == true)).
If there is no restriction, the generated pdf contains the images. Cool :).
But if I put a restriction, it doesn't. I understand that the mpdf object is the one which is asking to image function to show the image. So the verification failed.
I tried to use a config parameter on the CI instance object before I call the pdf function. For example, set to true a "pdf_conversion" and the conversion finished, set it back to false.
But it doesn't work. When I check the value in the image function, it's still false... It's like the config value is not change for all object but only for th pdf object which has changed it.
Is there a way to do what I want to do ??? With config parameter or anything else !!! I think I misunderstood something but don't know what !
thanks by advance for answers
Bastien
You problem is that then the library "calls" the image function, it is not logged in. So your controller code denies access.
There are two things you can do:
Move the code that generates/gets the image form the controller into a new library. then call that library from the controller and from the PDF library. This is the preferred way.
In the "image" action, find a way to check if the request is coming from the PDF library. Best guess is to look for something in the HTTP headers the PDF library sends. Anyway this is not very secure since HTTP headers can be "faked".
OFF TOPIC: Is there a reason you server the image from a controller action instead of by directly referencing the image file?
CLARIFICATION ON SOLUTION 1
So you'll have a library image.php that looks like this
class Image {
...
function get_image($hash){
[code for getting the image here]
}
...
}
In the controller:
function image($hash) {
$this->load->library('image');
if (if($this->session->userdata(‘logged’) == true)) {
$image = $this->image->get_image($hash);
[send it to the browser]
}
}
In the PDF library
function export($hash) {
$ci =& get_instace();
$ci->load->library('image');
[do pdf stuff]
$image = $ci->image->get_image($hash);
$this->add_image($image);
[do more pdf stuff]
}
This is the idea, the actual details of the implementation are up to you.