Simply Image Display Widget

Hi Guys,

Be really appreciated if someone could point me to the right direction.

Trying to create a widget to display images under the "Protected area" which usually involve "readfile", I can sort of get the function to work under when place under a controller, but as soon as I tried to make it into a re-usable widget, I am unable to get any image to display (but then again i am pretty new to widget and yii)

widget main file




<?php

class ProductImage extends CWidget {


    public function init()

    {

     

    }


    public function run() {

        $this->render('ProductImage_');

    }


    //returns html code with iamges.

    public function GetImagesForProduct()

    {

	 $path_to_file = "/opt/lampp/htdocs/project/protected/clientfolder/product/32/";

	 $imagefiles = $this->listImages($path_to_file); //returns in an array();


	 if(sizeof($imagefiles)>0)

	 {

	        foreach($imagefiles as $filename)

	       {

	        	echo "<img src='".$this->GetSingleImage($path_to_file.$filename)."'>";

	       }

	  }

        

    }


    public function GetSingleImage($path_to_file='')

    {

    	if(file_exists($path_to_file))

    	{	

    	    $img=getimagesize($path_to_file);

            header('Content-Type: '.$img['mime']);

            readfile($path_to_file);

    	}

    }


    public function listImages($dirname=".") {

	    $ext = array("jpg", "png", "jpeg", "gif");

	    $files = array();


	    if($handle = opendir($dirname)) {

		    while(false !== ($file = readdir($handle)))

			    for($i=0;$i<sizeof($ext);$i++)

			    	if(strstr($file, ".".$ext[$i]))

			    		$files[] = $file;

	     

	    	closedir($handle);

	    }


	    return($files);

    }




}

?>



The view file, neither of the two will work




<div id="productimage">   

    <?php 

    	echo $this->GetImagesForProduct();

    ?>

</div>






<div id="productimage">   

    <?php 

    	echo "<img scr='"$this->GetImagesForProduct()."'>";

    ?>

</div>



Output




The image cannot be displayed because it contains errors.



and if I remove the header('Content-Type: '.$img[‘mime’]); I will get what seems to be the image of a base64 string which I can’t display using base64_encode().

Thanks in advance

The code above cannot work. You are mixing different stuff.

You render a ‘img’ html tag:

the ‘src’ attribute has to be a a valid image url.

But your method ‘GetSingleImage’ returns no url.

So you have to create a controller action that outputs the image and you have to render img-tag with the url of the controller action as src.

You don’t need a CWidget for this.

Maybe something like below, not fully implemented or tested.




class ProductController extends CController 

{

  

    protected function outputImage($file)

    {

        //... code like your function GetSingleImage

    }


    public function listImages($dirname=".") 

    {

         //... like above 

    }


    //get the html img tags

    public function getImageUrls($product) 

    {

       $urls=array();

       $path_to_file = "/opt/lampp/htdocs/project/protected/clientfolder/product/$product/";

       $imagefiles = $this->listImages($path_to_file); 


       foreach($imagefiles as $file) 

        $urls[] = $this->createUrl('image',array('id'=$file); //the url of actionImage


       return $urls;    

    }




    public function actionImage($id) 

    {

       $this->outputImage($id); 

    }   


    

    public function actionImages($product) 

   {

      $this->render('productimages',array('product'=>$product);

   }





}







In you view ‘productimages’




  $imageUrls = $this->getImageUrls($product):


  foreach($imageUrls as $src)

    echo CHtml::image($src);




Hi Joblo,

Thanks heaps!! Sometimes it’s so frustrating because you still don’t know all about Yii, and it’s like poking in the dark. Took a second time of re-reading your message to come to realization yes, any internal links needs to be generated by a controller.

I will give that a shot now.