Welcome Guest, Not a member yet? Register   Sign In
How to extract an image url from a HTML text content and make it's thumbnail in Codeigniter
#1

[eluser]mi6crazyheart[/eluser]
It's a small function which you can use in your Codeigniter's MODEL Class to extract image link (image URL) from a HTML content. After extracting the image URL it extract the image from that URL & crop it as prescribed sizes.

Code:
function extractImageAndCreateThumb($htmlContent,$destinationToSave,$thumbHeight,$thumbWidth,$thumbName='')
    {
            //--- Set temporary folder path where extract image will be stored----            
            $tempFolderPathToSaveImg = "./assets/images/temp/";
            
            //-----extract the raw text with only img tag-----
            $Content = strip_tags(html_entity_decode($htmlContent),'<img>');
            
            // This will search for the src="" in the $Content variable
            $regular_expression = '~src="[^"]*"~';    
            
            // WE will grab all the images from the post in an array $allpics using preg_match_all
            preg_match_all( $regular_expression, $Content, $allpics );
            
            // Count the number of images found.
            $NumberOfPics = count($allpics[0]);
            
            //intialize value of the variable        
            $newFileName = '';
            
            if($NumberOfPics)
            {
                
                    for ( $i=0; $i < 1  ; $i++ )
                    {            
                        $str1=$allpics[0][$i];
                        $str1=trim($str1);
                        $len=strlen($str1);
                        $imgpath=substr_replace(substr($str1,5,$len),"",-1);
                    }
                    
                    $originalfileName = basename($imgpath);        // Get orignal image file name with extension
                    $path_parts = pathinfo($imgpath);                
                    $imageEXtension = $path_parts['extension'];    // Get image file name extension
                    
                    if($thumbName)
                    $newFileName = $thumbName.'.'.$imageEXtension ;
                    else                                                                            
                    $newFileName = $originalfileName ; //Create a new file name by adding BoardID to it
                    
                    $img = $this->curl->simple_get($imgpath);                    
                    if ( ! write_file($tempFolderPathToSaveImg.''.$newFileName, $img))
                    {
                          echo 'unable to write the file';
                    }
                
                                                              
                    $config['image_library'] = 'gd2';                    
                    $config['source_image']    = $tempFolderPathToSaveImg.''.$newFileName;                    
                    $config['maintain_ratio'] = TRUE;            
                    $config['new_image'] = $destinationToSave ;
                    $config['width']     = $thumbHeight;
                    $config['height']    = $thumbWidth;
            
                    $this->load->library('image_lib', $config);
                    if(!$this->image_lib->resize())
                    {
                        echo $this->image_lib->display_errors();
                    }                                    
                
                    // Delete the parent image which had stored temporarily
                    if( ! unlink($tempFolderPathToSaveImg.''.$newFileName) ) // Parent temp image deleted
                    {
                        echo "Couldn't delete Parent temp image";
                    }                    
            }
            
            //----- function return 1 if get any image else return 0----
            If($NumberOfPics)
            return 1;
            else
            return 0;
                
    }

Some more details: http://goo.gl/rUf8

You can download the codes from here also: http://www.box.net/shared/v3bn28my1x




Theme © iAndrew 2016 - Forum software by © MyBB