Yii Framework Forum: Simply Image Display Widget - Yii Framework Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Simply Image Display Widget Rate Topic: -----

#1 User is offline   jasocl 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 30
  • Joined: 08-March 12

Posted 14 January 2013 - 12:05 AM

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
0

#2 User is offline   Joblo 

  • Master Member
  • PipPipPipPip
  • Yii
  • Group: Members
  • Posts: 613
  • Joined: 12-September 10
  • Location:Austria

Posted 14 January 2013 - 03:32 AM

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


1

#3 User is offline   jasocl 

  • Junior Member
  • Pip
  • Yii
  • Group: Members
  • Posts: 30
  • Joined: 08-March 12

Posted 14 January 2013 - 04:46 PM

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.


View PostJoblo, on 14 January 2013 - 03:32 AM, said:

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



0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users