Support » Plugin: WP Open Graph » curl getimagesize alternative

  • Resolved Mad Max

    (@mad_max)


    On a host with allow_url_fopen=0 I get a lot of warnings on getimagesize() call in output.class.php, so I’ve added a method in class NY_OG_Output like this:

    	 /**
    	 * Retrieve remote image dimensions
    	 * - getimagesize alternative
    	 */
    	
    	/**
    	 * Get Image Size
    	 *
    	 * @param string $url
    	 * @param string $referer
    	 * @return array
    	 */
    	public function getimagesize_curl( $url, $referer = '' ) {
    		// Set headers
    		$headers = array( 'Range: bytes=0-131072' );
    		if ( !empty( $referer ) ) { array_push( $headers, 'Referer: ' . $referer ); }
    		
    		// Get remote image
    		$ch = curl_init();
    		curl_setopt( $ch, CURLOPT_URL, $url );
    		curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1 );
    		curl_setopt( $ch, CURLOPT_FOLLOWLOCATION, 1 );
    		$data = curl_exec( $ch );
    		$http_status = curl_getinfo( $ch, CURLINFO_HTTP_CODE );
    		$curl_errno = curl_errno( $ch );
    		curl_close( $ch );
    		
    		// Get network stauts
    		if ( $http_status != 200 ) {
    			//echo 'HTTP Status[' . $http_status . '] Errno [' . $curl_errno . ']';
    			return array(0,0);
    		}
    		
    		// Process image
    		$image = imagecreatefromstring( $data );
    		$dims = [ imagesx( $image ), imagesy( $image ) ];
    		imagedestroy($image);
    		
    		return $dims;
    	}

    and changed line 308 from this

    $size = @getimagesize( $image_path );

    to this

    if( ini_get('allow_url_fopen') ) {
    		$size = getimagesize( $image_path );
    	}else{
    		$size = $this->getimagesize_curl( $image_path );
    	}

    Hope you can consider to add something similar ia future release.
    Thanks

    • This topic was modified 2 years, 3 months ago by Mad Max.
    • This topic was modified 2 years, 3 months ago by Mad Max.
Viewing 2 replies - 1 through 2 (of 2 total)
Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘curl getimagesize alternative’ is closed to new replies.