M
M
My joy2014-10-07 20:26:01
PHP
My joy, 2014-10-07 20:26:01

How to shrink an image in imagemagick using php?

You need to make a mini-restapi to reduce the photos on your site. I throw the image url as a get-parameter into the method, and I need to somehow tell imagemagick that it needs to be reduced. Tell me who worked with him, how to do it? Thank you.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
K
KorsaR-ZN, 2014-10-07
@t-alexashka

imagemagick - as a console utility or PHP module?
for console utility , for module

S
Sharov Dmitry, 2014-10-08
@vlom88

I did it in one parser like this

$imgUrl = 'http://www.site.ru/img/path/img.jpg';
$i = file_get_contents($imgUrl);
if(strpos($imgUrl, '.jpg'))      $ext = '.jpg';
elseif(strpos($imgUrl, '.png'))  $ext = '.png';
elseif(strpos($imgUrl, '.jpeg')) $ext = '.png';
elseif(strpos($imgUrl, '.gif'))  $ext = '.gif';
$filename = md5(rand(10000, 99999999)) . $ext;
file_put_contents(CRON_DIR . 'parse_data/img/' . $filename, $i);
$rgImages = upload_images('parse_data/img/' . $filename, $ext)
unlink(CRON_DIR . 'parse_data/img/' . $filename);

function upload_images($img, $ext){
        $folder = '/UPLOAD/'.date('Y/m/d') . '/';
        if(!is_dir(ROOT_DIR.$folder)){
            mkdir(ROOT_DIR.$folder, 0777, true);
        }
        
        $rgFiles = array();
        
        $orig = $folder.md5(time().mt_rand(0,999999)).$ext; 
        
        $img = file_get_contents(CRON_DIR.$img);
        file_put_contents(ROOT_DIR.$orig, $img);
        chmod(ROOT_DIR.$orig, 0777); 
        
        $rgFiles['sys_thumb']     = make_resize_image(ROOT_DIR.$orig, $ext, 75, 75);
        $rgFiles['preview']       = make_resize_image(ROOT_DIR.$orig, $ext, 800, 600);
        $rgFiles['thumb']         = make_resize_image(ROOT_DIR.$orig, $ext, 200, 200, 0);
        $rgFiles['thumb2']        = make_resize_image(ROOT_DIR.$orig, $ext, 364, 364);
        $rgFiles['original']      = $orig;
        
        return serialize($rgFiles);
    }
    
function make_resize_image($orig_filename, $ext, $w1, $h1, $crop = 1){
        
        $dir = '/UPLOAD/'.date('Y/m/d') . '/';
        if(!is_dir(ROOT_DIR.$dir)){
            mkdir(ROOT_DIR.$dir, 0777, true);
        }
        
        do {
            $filename = md5(time().mt_rand(0,999999)).$ext; 
        } while (file_exists(ROOT_DIR.$dir.$filename));
        
        $file = $dir.$filename;
        
        list($w0,$h0)=getimagesize($orig_filename);
    	if($w1!=0 && $h1!=0 && !$crop){
        	$p0=$w0/$h0;
        	$p1=$w1/$h1;
        				
        	if($p0>$p1)$h1=0;
        	else $w1=0;			
    	}
        
    	if(class_exists('Imagick')){
    		$thumb = new Imagick();
    		$thumb->readImage($orig_filename);
    		if($crop) $thumb->cropThumbnailImage($w1, $h1);
    		else $thumb->thumbnailImage($w1, $h1);
    		$thumb->writeImage(ROOT_DIR.$file);
    		$thumb->destroy();
    	}  
    	 
    	chmod(ROOT_DIR.$file, 0777);
    	
    	return $file;
    }

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question