Heads up! This post was written 13 years ago. Some information might be outdated or may have changed since then.
В един от сайтове по които работя имат навика да качват 2-3 MB снимки които после чрез редактора ги "смаляват" което не е добра идея. Та за да предотвратя тегленето на излишна информация използвам следното скриптче:
function html_fix($html_string){
    $work_url = 'http://images.yuksbg.net/';
    $work_dir = '/home/yuks/images/';
    $convert_path = 'convert';

    preg_match_all('/]*>/im',$html_string,$matches);
    if(!count($matches)) { 
        return $html_string; 
    } 
    foreach($matches[0] as $match){ 
        $width=0; 
        $height=0; 
        
        if(preg_match('/width="[0-9]*"/i',$match) && preg_match('/height="[0-9]*"/i',$match)){ 
            $width=preg_replace('/.*width="([0-9]*)".*/i','\1',$match); 
            $height=preg_replace('/.*height="([0-9]*)".*/i','\1',$match); 
        } 
        else if(preg_match('/style="[^"]*width: *[0-9]*px/i',$match) && preg_match('/style="[^"]*height: *[0-9]*px/i',$match)){ 
            $width=preg_replace('/.*style="[^"]*width: *([0-9]*)px.*/i','\1',$match); 
            $height=preg_replace('/.*style="[^"]*height: *([0-9]*)px.*/i','\1',$match); 
        } 
        
        if(!$width || !$height)continue; 
        
        $imgsrc=preg_replace('/.*src="([^"]*)".*/i','\1',$match); 
        
        if(!preg_match('/^http/i',$imgsrc))$imgsrc=preg_replace('#^/*#','http://'.$_SERVER['HTTP_HOST'].'/',$imgsrc); 
        
        list($x,$y)=getimagesize($imgsrc); 
        
        if(!$x || !$y || ($x==$width && $y==$height)) { 
            continue; 
        }; 
 		
        $dir=md5($imgsrc); 
        $newURL= $work_url.$dir.'/'.$width.'x'.$height.'.jpg'; 
        $newImgHTML=preg_replace('/(.*src=")[^"]*(".*)/i',"$1$newURL$2",$match); 
        $html_string=str_replace($match,$newImgHTML,$html_string); 
        $imgdir= $work_dir.$dir; 
        @mkdir($imgdir); 
        $imgfile=$imgdir.'/'.$width.'x'.$height.'.jpg'; 
        if(file_exists($imgfile)) { 
            continue; 
        }; 
        
        $str= $convert_path.' "'.addslashes($imgsrc).'" -geometry '.$width.'x'.$height.' "'.$imgfile.'"'; 
        exec($str); 
    } 
    
    return $html_string; 
}

идеята е ако видим html tag img и в него срещнем style="width...... или width="....." да проверим дали вече не сме създали такова изображение и ако не сме създаваме и след това replace в html то :)
Едва ли е най-елегантното решение но конкретно при моя случай се постига искания ефект.

Back to all posts