-1
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); 

if ( ! function_exists('image_thumb')){
    function image_thumb($courseBanner, $userId){
        $CI =& get_instance();
        $filename_ext = pathinfo($courseBanner, PATHINFO_EXTENSION);
        $course_thumb_Banner = preg_replace('/^(.*)\.' . $filename_ext . '$/', '$1_thumb.' . $filename_ext, $courseBanner);
        $url =  base_url()."private/".$userId."/".$course_thumb_Banner;
        $headers=get_headers($url);
        $is_banner_there  =  stripos($headers[0],"200 OK")?true:false;
        $url = ($is_banner_there)?$url: base_url()."public/images/placeholder.jpg";
        return $url;
    }
}

In the above code sometimes $courseBanner doesn't come, so that time I want to send this base_url()."public/images/placeholder.jpg How to do?

Kevin
  • 653
  • 2
  • 13
  • 34

1 Answers1

1

Suggestion:

I think you can set a default value if in case the string (e.g $courseBanner) is empty

Example:

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

if ( ! function_exists('image_thumb')){
    function image_thumb($userId, $courseBanner = ''){
        $CI =& get_instance();
        $courseBanner = !empty($courseBanner) ? $courseBanner : base_url()."public/images/placeholder.jpg";
        $filename_ext = pathinfo($courseBanner, PATHINFO_EXTENSION);
        $course_thumb_Banner = preg_replace('/^(.*)\.' . $filename_ext . '$/', '$1_thumb.' . $filename_ext, $courseBanner);
        $url =  base_url()."private/".$userId."/".$course_thumb_Banner;
        $headers=get_headers($url);
        $is_banner_there  =  stripos($headers[0],"200 OK")?true:false;
        return $url;
    }
}

This is for reference only so hope this helps

lazyCoder
  • 2,544
  • 3
  • 22
  • 41
Marylyn Lajato
  • 1,171
  • 1
  • 10
  • 15
  • if only one argument is passed it will throw error as it will take that one argument for `coursebanner`, all optional arguments are put in last – M A SIDDIQUI Apr 17 '17 at 13:11
  • @eeya make a note that optional parameter always come at the end so please correct your function structure to function image_thumb($userId, $courseBanner = ' ') – lazyCoder Apr 17 '17 at 13:14
  • @Bunker Boy, @M A SIDDIQUI .Noted. I'll edit this answer – Marylyn Lajato Apr 17 '17 at 13:16
  • @BunkerBoy but now all courses takes placeholder image. – Kevin Apr 17 '17 at 13:20
  • @eeya do it fast else i ll will also down vote your answer – lazyCoder Apr 17 '17 at 13:20
  • @BunkerBoy . Edited the answer. Sorry about that – Marylyn Lajato Apr 17 '17 at 13:23
  • @Kevin . What happens if you've called your method like this? image_thumb($userId, '/sample/url/image'); – Marylyn Lajato Apr 17 '17 at 13:33
  • its taking only `if (is_string($courseBanner) !== true && strlen($courseBanner) <= 0) { $courseBanner = base_url() . "public/images/placeholder.jpg"; }` – Kevin Apr 17 '17 at 13:42
  • @BunkerBoy at this point its working correctly `function image_thumb($userId, $courseBanner = ''){ $CI =& get_instance(); $courseBanner = !empty($courseBanner) ? $courseBanner : base_url()."public/images/placeholder.jpg"; return $courseBanner; }` – Kevin Apr 17 '17 at 14:08
  • @Kevin tick mark this answer as correct, if it worked for you (y) – lazyCoder Apr 17 '17 at 14:10