wp-content/plugins/resmushit-image-optimizer/classes/resmushit.class.php
/**
	 *
	 * Optimize a picture according to a filepath.
	 *
	 * @param  string $file_path the path to the file on the server
	 * @return bool 	TRUE if the resmush operation worked
	 */
Optimize a picture according to a filepath.
@param  string $file_path the path to the file on the server
@return bool         TRUE if the resmush operation worked
Optimize a picture according to a filepath . @param string $file_path the path to the file on the server @return bool TRUE if the resmush operation worked
public static function optimize($file_path = NULL, $is_original = TRUE) {
		global $wp_version;
		if(filesize($file_path) > self::MAX_FILESIZE){
			rlog('Error! Picture ' . $file_path . ' cannot be optimized, file size is above 5MB ('. reSmushitUI::sizeFormat(filesize($file_path)) .')');
			return false;
		}
		$ch = curl_init();
		curl_setopt($ch, CURLOPT_URL, RESMUSHIT_ENDPOINT);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
		curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, RESMUSHIT_TIMEOUT);
		curl_setopt($ch, CURLOPT_POST, true);
		curl_setopt($ch, CURLOPT_USERAGENT, "Wordpress $wp_version/Resmush.it " . RESMUSHIT_VERSION . ' - ' . get_bloginfo('wpurl') );
		if (!class_exists('CURLFile')) {
			$arg = array('files' => '@' . $file_path);
		} else {
			$cfile = new CURLFile($file_path);
			$arg = array(
			  'files' => $cfile,
			);
		}
		$arg['qlty'] = self::getPictureQualitySetting();
		curl_setopt($ch, CURLOPT_POSTFIELDS, $arg);
		$data = curl_exec($ch);
		curl_close($ch);
		$json = json_decode($data);
		if($json){
			if (!isset($json->error)) {
				$data = file_get_contents($json->dest);
				if ($data) {
					if($is_original){
						$originalFile = pathinfo($file_path);
						$newPath = $originalFile['dirname'] . '/' . $originalFile['filename'] . '-unsmushed.' . $originalFile['extension'];
			 			copy($file_path, $newPath);
			 		}
				  	file_put_contents($file_path, $data);
					rlog("Picture " . $file_path . " optimized from " . reSmushitUI::sizeFormat($json->src_size) . " to " . reSmushitUI::sizeFormat($json->dest_size));
				  	return $json;
				}
			} else {
				rlog("Webservice returned the following error while optimizing $file_path : Code #" . $json->error . " - " . $json->error_long);
			}
		} else {
			rlog("Cannot establish connection with reSmush.it webservice while optimizing $file_path (timeout of " . RESMUSHIT_TIMEOUT . "sec.)");
		}
		return false;
	}
public static function optimize ( $ file_path = NULL , $ is_original = TRUE ) { global $ wp_version ; if ( filesize ( $ file_path ) > self :: MAX_FILESIZE ) { rlog ( 'Error! Picture ' . $ file_path . ' cannot be optimized, file size is above 5MB (' . reSmushitUI :: sizeFormat ( filesize ( $ file_path ) ) . ')' ) ; return false ; } $ ch = curl_init ( ) ; curl_setopt ( $ ch , CURLOPT_URL , RESMUSHIT_ENDPOINT ) ; curl_setopt ( $ ch , CURLOPT_RETURNTRANSFER , 1 ) ; curl_setopt ( $ ch , CURLOPT_CONNECTTIMEOUT , RESMUSHIT_TIMEOUT ) ; curl_setopt ( $ ch , CURLOPT_POST , true ) ; curl_setopt ( $ ch , CURLOPT_USERAGENT , " Wordpress  $ wp_version /Resmush.it  " . RESMUSHIT_VERSION . ' - ' . get_bloginfo ( 'wpurl' ) ) ; if ( ! class_exists ( 'CURLFile' ) ) { $ arg = array ( 'files' => '@' . $ file_path ) ; } else { $ cfile = new CURLFile ( $ file_path ) ; $ arg = array ( 'files' => $ cfile , ) ; } $ arg [ 'qlty' ] = self :: getPictureQualitySetting ( ) ; curl_setopt ( $ ch , CURLOPT_POSTFIELDS , $ arg ) ; $ data = curl_exec ( $ ch ) ; curl_close ( $ ch ) ; $ json = json_decode ( $ data ) ; if ( $ json ) { if ( ! isset ( $ json -> error ) ) { $ data = file_get_contents ( $ json -> dest ) ; if ( $ data ) { if ( $ is_original ) { $ originalFile = pathinfo ( $ file_path ) ; $ newPath = $ originalFile [ 'dirname' ] . '/' . $ originalFile [ 'filename' ] . '-unsmushed.' . $ originalFile [ 'extension' ] ; copy ( $ file_path , $ newPath ) ; } file_put_contents ( $ file_path , $ data ) ; rlog ( " Picture  " . $ file_path . "  optimized from  " . reSmushitUI :: sizeFormat ( $ json -> src_size ) . "  to  " . reSmushitUI :: sizeFormat ( $ json -> dest_size ) ) ; return $ json ; } } else { rlog ( " Webservice returned the following error while optimizing  $ file_path  : Code # " . $ json -> error . "  -  " . $ json -> error_long ) ; } } else { rlog ( " Cannot establish connection with reSmush.it webservice while optimizing  $ file_path  (timeout of  " . RESMUSHIT_TIMEOUT . " sec.) " ) ; } return false ; }
Optimize a picture according to a filepath.
Optimize a picture according to a filepath .