Posting in the Magento forums has been disabled pending the implementation of a new and improved forum solution which should better serve the community.

For new questions please post at magento.stackexchange.com, the community-run support site for the Magento community. We will be providing updates on the new forum solution soon. For questions or concerns please email community@magento.com.

Magento Forum

Reworking directResize so that it doesn’t need “allow_url_fopen On” in php.ini
 
Core
Member
 
Total Posts:  68
Joined:  2008-03-09
 

Hi,

I’m using directResize and it needs “allow_url_fopen” set to true.  However, this is apparently bad practice as it’s a bit of a security hole.
Either directResize needs to full path passed to getimagesize or we need to use curl.

I’m only a beginner with magento and there might be an easier way.  I’ve changed the directresize function in image.php in the NetAmbitions package so that at the top of the file it’s now this:

$ch = curl_init();
$timeout = 5; // set to zero for no timeout
curl_setopt ($ch, CURLOPT_URL, $this);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$file_contents = curl_exec($ch);
// close cURL resource, and free up system resources
curl_close($ch);

$new_image = ImageCreateFromString($file_contents);
imagejpeg($new_image, ‘tmp.jpg’,100);

$size = getimagesize(’tmp.jpg’);
$img_src_w = $size[0];
$img_src_h = $size[1];

And this actually works.  BUT, tmp.jpg needs to be written onto the file server and I’m not sure if it’s particularly efficient.  Also what happens when lots of people start browsing the product pages? As we only want the x & the y sizes is there a better way so that it’s just done in memory?  or can I do it without using curl and circumvent the need for allow_url_fopen to be turned on?

any ideas?

 
Magento Community Magento Community
Magento Community
Magento Community
 
mike222
Member
 
Total Posts:  41
Joined:  2008-04-07
Austria
 

In my case it was not only a security issue, it is simple not possible to access the images via the official url, because the server is behind a firewall and has only private ip-adresses. The url from the customer’s point of view is not valid for the php scripts.

Therefore I did a string-replace to convert all possible urls to the actual physical directories.

Something like

$url = $this->__toString();
$url = str_replace("http://xx.xx.xx.xx/shop", “/srv/www/magento”, $url);
$url = str_replace("http://my.domain.at/shop", “/srv/www/magento”, $url);
$size = getimagesize($url);

helped me out.

regards,

Mike

 
Magento Community Magento Community
Magento Community
Magento Community
 
Core
Member
 
Total Posts:  68
Joined:  2008-03-09
 

I’ve settled on the following CURL based option.

One of the disadvantages of using the str_replace method is that if your website is ever moved for any reason all the images
would be broken.  Also it’s not a generic solution that anyone can pick up without having to modify the source for their installation.

This version doesn’t save the file it’s kept in memory.  It could be made quicker by just reading the header of the file and working out the dimensions, I’m not sure if CURL would allow you to retrieve just the bit you want.  For the moment I’m sticking with this solution.

$ch = curl_init();
curl_setopt ($ch, CURLOPT_URL, $this->__toString());
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, true);
$file_contents = curl_exec($ch);
curl_close($ch);

if (!is_string($file_contents) || !isset($file_contents[0])) {
return $this->resize($img_min_w, $img_min_h);
}

$image = ImageCreateFromString($file_contents);

$img_src_w = imagesx($image);
$img_src_h = imagesy($image);

 
Magento Community Magento Community
Magento Community
Magento Community
 
Thejosh13
Sr. Member
 
Total Posts:  252
Joined:  2008-06-23
 

Can someone help with this? I put in the code but I think it may be in the wrong format..

Warning: getimagesize() [function.getimagesize]: URL file-access is disabled in the server configuration in app/code/local/NetAmbition/DirectResize/Helper/Image.php on line 9

--------------------------------
<?php

class NetAmbition_DirectResize_Helper_Image extends Mage_Catalog_Helper_Image
{

public function directResize($img_min_w, $img_min_h, $ratio=0, $autoriseAgrandissement = false)
{
$ch = curl_init();
curl_setopt ($ch, CURLOPT_URL, $this->__toString());
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, true);
$file_contents = curl_exec($ch);
curl_close($ch);

if (!is_string($file_contents) || !isset($file_contents[0])) {
return $this->resize($img_min_w, $img_min_h);
}

$image = ImageCreateFromString($file_contents);

$img_src_w = imagesx($image);
$img_src_h = imagesy($image);
//--> On fait le calcul de l'image que si les dimensions demandées sont inférieures à celles d'origine
if($img_min_w < $img_src_w || $img_min_h < $img_src_h || $autoriseAgrandissement){
//--> Calcul des dimensions de l'image à afficher suivant le ratio choisi
switch ($ratio){
case 0:  //-- redimensionnement aux dimentions exactes données
$img_min_w_calc = $img_min_w;
$img_min_h_calc = $img_min_h;
break;
case 1: //-- redimensionnement largeur fixée et hauteur calculée
$img_min_w_calc = $img_min_w;
$img_min_h_calc = round($img_src_h * $img_min_w_calc / $img_src_w);
break;
case 2: //-- redimensionnement hauteur fixée et largeur calculée
$img_min_h_calc = $img_min_h;
$img_min_w_calc = round($img_src_w * $img_min_h_calc / $img_src_h);
break;
case 3: //-- redimensionnement pour que l'image rentre proportionnellement dans la largeur et la heuteur fixées
$ratio_wh = $img_src_w / $img_src_h;
$ratio_whmin = $img_min_w / $img_min_h;
if ($ratio_wh > $ratio_whmin){
$img_min_w_calc = $img_min_w;
$img_min_h_calc = round($img_src_h * $img_min_w_calc / $img_src_w);
} else {
$img_min_h_calc = $img_min_h;
$img_min_w_calc = round($img_src_w * $img_min_h_calc / $img_src_h);
}
break;
case 4: //-- redimensionnement pour que l'image couvre au plus juste la hauteur et la largeur fixées
if ($img_src_w/$img_src_h > $img_min_w/$img_min_h) {
$img_min_h_calc = $img_min_h;
$img_min_w_calc = round($img_src_w * $img_min_h_calc / $img_src_h);
} else {
$img_min_w_calc = $img_min_w;
$img_min_h_calc = round($img_src_h * $img_min_w_calc / $img_src_w);
}
break;
}
//--> On continue la procédure d'affichage de la miniature par Magento
$this->resize($img_min_w_calc, $img_min_h_calc);
}
return $this;
}
}

?>
---------------------

Line 9 is large text. Help?

 
Magento Community Magento Community
Magento Community
Magento Community
 
Thejosh13
Sr. Member
 
Total Posts:  252
Joined:  2008-06-23
 

Still cant figure it out, anybody?

 
Magento Community Magento Community
Magento Community
Magento Community
 
davidgrun
Sr. Member
 
Total Posts:  245
Joined:  2008-07-10
 

@Core

Thank you! This worked!

Here is my working file looks like

<?php

class NetAmbition_DirectResize_Helper_Image extends Mage_Catalog_Helper_Image
{

    
public function directResize($img_min_w$img_min_h$ratio=0$autoriseAgrandissement false)
    
{
        
// [ CUSTOMIZATION ] Based on post #1 on http://www.magentocommerce.com/boards/viewthread/8388/

        
$ch curl_init();
        
$timeout 5// set to zero for no timeout
        
curl_setopt ($chCURLOPT_URL$this);
        
curl_setopt ($chCURLOPT_RETURNTRANSFER1);
        
curl_setopt ($chCURLOPT_CONNECTTIMEOUT$timeout);
        
$file_contents curl_exec($ch);
        
// close cURL resource, and free up system resources
        
curl_close($ch);
        
        
$new_image ImageCreateFromString($file_contents);
        
imagejpeg($new_image'tmp.jpg',100);
        
        
$size getimagesize('tmp.jpg');
        
$img_src_w $size[0];
        
$img_src_h $size[1]
        
        
        
//--> Recherche des dimentions de l'image d'origine
        //$size             = getimagesize($this);
        //$img_src_w         = $size[0];
        //$img_src_h         = $size[1];

        // [END CUSTOMIZATION]


        //--> On fait le calcul de l'image que si les dimensions demandées sont inférieures Ã  celles d'origine
        
if($img_min_w $img_src_w || $img_min_h $img_src_h || $autoriseAgrandissement){
            
//--> Calcul des dimensions de l'image Ã  afficher suivant le ratio choisi
            
switch ($ratio){
                
case 0:    //-- redimensionnement aux dimentions exactes données
                    
$img_min_w_calc    $img_min_w;
                    
$img_min_h_calc    $img_min_h;
                    break;
                case 
1//-- redimensionnement largeur fixée et hauteur calculée
                    
$img_min_w_calc    $img_min_w;
                    
$img_min_h_calc    round($img_src_h $img_min_w_calc $img_src_w);
                    break;
                case 
2//-- redimensionnement hauteur fixée et largeur calculée
                    
$img_min_h_calc    $img_min_h;
                    
$img_min_w_calc    round($img_src_w $img_min_h_calc $img_src_h);
                    break;
                case 
3//-- redimensionnement pour que l'image rentre proportionnellement dans la largeur et la heuteur fixées
                    
$ratio_wh        $img_src_w $img_src_h;
                    
$ratio_whmin    $img_min_w $img_min_h;
                    if (
$ratio_wh $ratio_whmin){
                        $img_min_w_calc    
$img_min_w;
                        
$img_min_h_calc    round($img_src_h $img_min_w_calc $img_src_w);
                    
else {
                        $img_min_h_calc    
$img_min_h;
                        
$img_min_w_calc    round($img_src_w $img_min_h_calc $img_src_h);
                    
}
                    
break;
                case 
4//-- redimensionnement pour que l'image couvre au plus juste la hauteur et la largeur fixées
                    
if ($img_src_w/$img_src_h $img_min_w/$img_min_h{
                        $img_min_h_calc    
$img_min_h;
                        
$img_min_w_calc    round($img_src_w $img_min_h_calc $img_src_h);
                    
else {
                        $img_min_w_calc    
$img_min_w;
                        
$img_min_h_calc    round($img_src_h $img_min_w_calc $img_src_w);
                    
}
                    
break;
            
}
            
//--> On continue la procédure d'affichage de la miniature par Magento
            
$this->resize($img_min_w_calc$img_min_h_calc);
        
}
        
return $this;
    
}
}

?>
 
Magento Community Magento Community
Magento Community
Magento Community
 
CreedFeed
Member
 
Total Posts:  74
Joined:  2007-08-31
Milwaukee, WI
 

See this thread http://www.magentocommerce.com/boards/viewthread/8443/

Replace:

public function directResize($img_min_w$img_min_h$ratio=0$autoriseAgrandissement false)
    
{
        
//--> Recherche des dimentions de l'image d'origine
        
$size             getimagesize($this);
        
$img_src_w         $size[0];
        
$img_src_h         $size[1];

With:

public function directResize($img_min_w$img_min_h$ratio=0$autoriseAgrandissement false)
    
{
        
//--> Recherche des dimentions de l'image d'origine
        
$url            parse_url($this);
        
$cwd            getcwd();
        
$size             getimagesize($cwd $url['path']);
        
$img_src_w         $size[0];
        
$img_src_h         $size[1];

This seems to work for me, at least for the category view pages. My problem was that using Direct Size out of the box on my server was causing slowness. I pinpointed it to be because of the getimagesize() function. By pulling from the local file server versus a URL, it sped my site up dramatically.

 
Magento Community Magento Community
Magento Community
Magento Community
 
davidgrun
Sr. Member
 
Total Posts:  245
Joined:  2008-07-10
 

@CreedFeed

Awesome, thanks! This seems to work great for me too. I downloaded the FireFox addon ¨Lori¨ to track page load time and compared it.

This is the final file:

<?php

class NetAmbition_DirectResize_Helper_Image extends Mage_Catalog_Helper_Image
{

    
public function directResize($img_min_w$img_min_h$ratio=0$autoriseAgrandissement false)
    
{
        
// [ CUSTOMIZATION ] Base on post #1 on http://www.magentocommerce.com/boards/viewthread/8388/
        
$ch curl_init();
        
$timeout 5// set to zero for no timeout
        
curl_setopt ($chCURLOPT_URL$this);
        
curl_setopt ($chCURLOPT_RETURNTRANSFER1);
        
curl_setopt ($chCURLOPT_CONNECTTIMEOUT$timeout);
        
$file_contents curl_exec($ch);
        
// close cURL resource, and free up system resources
        
curl_close($ch);
        
        
$new_image ImageCreateFromString($file_contents);
        
imagejpeg($new_image'tmp.jpg',100);
        
        
        
// [further COSTUMIZATION] Base on post #6 on http://www.magentocommerce.com/boards/viewthread/8388/#t45901
        
        //$size = getimagesize('tmp.jpg');
        //$img_src_w = $size[0];
        //$img_src_h = $size[1]; 
        
        
$url            parse_url($this);
        
$cwd            getcwd();
        
$size             getimagesize($cwd $url['path']);
        
$img_src_w         $size[0];
        
$img_src_h         $size[1];
        
        
//--> Recherche des dimentions de l'image d'origine
        //$size             = getimagesize($this);
        //$img_src_w         = $size[0];
        //$img_src_h         = $size[1];

        // [END CUSTOMIZATION]


        //--> On fait le calcul de l'image que si les dimensions demandées sont inférieures Ã  celles d'origine
        
if($img_min_w $img_src_w || $img_min_h $img_src_h || $autoriseAgrandissement){
            
//--> Calcul des dimensions de l'image Ã  afficher suivant le ratio choisi
            
switch ($ratio){
                
case 0:    //-- redimensionnement aux dimentions exactes données
                    
$img_min_w_calc    $img_min_w;
                    
$img_min_h_calc    $img_min_h;
                    break;
                case 
1//-- redimensionnement largeur fixée et hauteur calculée
                    
$img_min_w_calc    $img_min_w;
                    
$img_min_h_calc    round($img_src_h $img_min_w_calc $img_src_w);
                    break;
                case 
2//-- redimensionnement hauteur fixée et largeur calculée
                    
$img_min_h_calc    $img_min_h;
                    
$img_min_w_calc    round($img_src_w $img_min_h_calc $img_src_h);
                    break;
                case 
3//-- redimensionnement pour que l'image rentre proportionnellement dans la largeur et la heuteur fixées
                    
$ratio_wh        $img_src_w $img_src_h;
                    
$ratio_whmin    $img_min_w $img_min_h;
                    if (
$ratio_wh $ratio_whmin){
                        $img_min_w_calc    
$img_min_w;
                        
$img_min_h_calc    round($img_src_h $img_min_w_calc $img_src_w);
                    
else {
                        $img_min_h_calc    
$img_min_h;
                        
$img_min_w_calc    round($img_src_w $img_min_h_calc $img_src_h);
                    
}
                    
break;
                case 
4//-- redimensionnement pour que l'image couvre au plus juste la hauteur et la largeur fixées
                    
if ($img_src_w/$img_src_h $img_min_w/$img_min_h{
                        $img_min_h_calc    
$img_min_h;
                        
$img_min_w_calc    round($img_src_w $img_min_h_calc $img_src_h);
                    
else {
                        $img_min_w_calc    
$img_min_w;
                        
$img_min_h_calc    round($img_src_h $img_min_w_calc $img_src_w);
                    
}
                    
break;
            
}
            
//--> On continue la procédure d'affichage de la miniature par Magento
            
$this->resize($img_min_w_calc$img_min_h_calc);
        
}
        
return $this;
    
}
}

?>
 
Magento Community Magento Community
Magento Community
Magento Community
Magento Community
Magento Community
Back to top