Conditional Image Thumbnail

Permalink
Hi all,

I'm trying to create a page list template with thumbnails, the issue I'm having is that when a page does not have an image, I get an error:

Fatal error: Call to a member function getRelativePath() on a non-object in /mysite/blocks/page_list/templates/new-template/view.php on line 27


I know it's caused by those pages that do not have page thumbnails.

I tried following this thread:http://www.concrete5.org/community/forums/customizing_c5/custom-pag...

but I just can't understand what I need to change in my view.php to get this to work. I attached my view.php as well (it's the original page-list view.php with the image attributes added in.

I appreciate all of your help.

1 Attachment

 
fastcrash replied on at Permalink Reply
fastcrash
why not check it if img exist?

<?php
if (file_exists('./' . $img_src')) {   //set for root dir
?>
<div class="event-photo">
<img src="<?php  echo $img_src ?>"/>
</div>
<?php
}
?>
/*
nb: you should check in this too
if(isset($img->getRelativePath())){
$img_src = $img->getRelativePath(); //i think the error come from this first or $img = $page->getAttribute('thumbnail')?
*/
}
popscomp replied on at Permalink Reply
I'm sorry, but I just don't understand what's trying to be accomplished in that code at all. I understand I need an if statement, I just don't know where to put it, as the error line is on line 27.

I tried to put some of that code in my view.php, but wasn't sure how to fix it.
fastcrash replied on at Permalink Reply
fastcrash
change line 27
$img_src = $img->getRelativePath();


with this
if(isset($img->getRelativePath())){
$img_src = $img->getRelativePath();
}else{
$img_src = 'some/default/thumbnail/here';
}


see if the error still occur //if i not wrong :)
popscomp replied on at Permalink Reply
Thanks for your help fastcrash,

unfortunately, that code gives me an error:

Fatal error: Can't use method return value in write context in view.php on line 27
fastcrash replied on at Permalink Reply
fastcrash
how about this

if($img){  //just change this
$img_src = $img->getRelativePath();
}else{
$img_src = 'some/default/thumbnail/here';
}


sory, i'm not pro after all , hahaa
popscomp replied on at Permalink Reply
fastcrash,

Unfortunately, that one gave me this error:

Fatal error: Call to a member function getPath() on a non-object in /site.com/blocks/page_list/templates/events/view.php on line 33

Line 33 now is:

list($img_width, $img_height) = getimagesize($img->getPath());


Ideally, I'd like to have just the page title, and no default thumb if there's none for the page.

Thanks again for your help!
fastcrash replied on at Permalink Reply
fastcrash
maybe like this

if($img){  //just change this
$img_src = $img->getRelativePath();
list($img_width, $img_height) = getimagesize($img->getPath());
}
beebs93 replied on at Permalink Best Answer Reply
beebs93
Simply check if $img is a valid File object:

$img = $page->getAttribute('thumbnail');
if(!is_object($img) || !$img instanceof File){
   /* No thumbnail image available */
  $event_photo = '';
}else{
  $img_src = $img->getRelativePath();
  /* Skip using getimagesize() */
  $img_width = $img->getAttribute('width');
  $img_height = $img->getAttribute('height');
  $event_photo = '<img src="' . $img_src . '" width="' . $img_width . '" height="' . $img_height . '" alt="Event photo" />';
}
popscomp replied on at Permalink Reply
Thank you so much beebs93! That did it!

Sorry, I missed the closing brace on my copy..lol