0

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

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Can you please give me a code example ? I don't understand what you said. Where should I extend HtmlHelper ? Should I change the logic of the main "image()" method in the HtmlHelper.php file ? I tried that, but I can't get the image's path there. I thought to check the path, if an image exists, its ok, if not, I'll define a default image. But I don't understand how to check the path & where to define the default image. By the way, thank a lot for your response. Just help me a little more. Thanks. @burzum
Sorry, no. I'm not going to do your job. I gave you enough information and you, as a developer, should be able to solve this quickly, it is basic object oriented programming. If you don't know how to override methods take a lesson in OOP programming or ask a question here on SO related to it.
0

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.)

2 Comments

Yeah, that's a good idea, but I also thought about it. I think it'll give me & the server much extra load. Suppose, I want to use this in everywhere in my application. So, I have to do these extra checking always. That's very disgusting I think. And, think about the server. It also always has to do this extra processing(checking). I am actually looking for an easier way, because I want to use this always in my application where there is an image. Anyway, thanks a lot for your help.
Your whole concept is flawed then because the way you do it you have to check if the image exists. Instead you should rely on the information about that image: If there is no image for whatever you lookup in your db display the default image. If you have to check that the image really exists on disk, well then there is no way around that file_exsts() check. However my FileStorage plugin provides a complete solution for most, if not all, issues related to file uploads, storage and processing and display of images. github.com/burzum/FileStorage

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.