Support » Plugin: PNG to JPG » PNG to JPG conversion bug on duplicity extensions’jpe’, ‘jif’, ‘jfi’, ‘jfif’

  • Resolved payter

    (@payter)


    Hi, i’m wondering, why when duplicity ocurrs, this plugin change extension, instead of setting sequence of duplicity, example:

    test.png > test.jpg
    test.png > test.jpeg
    test.png > test.jpe
    test.png > test.jif
    test.png > test.jfi
    test.png > test.jfif
    test.png > infinite loop…

    correct behaviour should be:

    test.png > test.jpg
    test.png > test-1.jpg
    test.png > test-2.jpg
    test.png > test-3.jpg

    here is updated convert_image() method by me to fix this issue, please update your plugin with this fix, thank you!

    
    	function convert_image( $params ){
    		$stats_before = filesize( $params['file'] );
    		$img = imagecreatefrompng( $params['file'] );
    		$bg = imagecreatetruecolor( imagesx( $img ), imagesy( $img ) );
    		imagefill( $bg, 0, 0, imagecolorallocate( $bg, 255, 255, 255 ) );
    		imagealphablending( $bg, 1 );
    		imagecopy( $bg, $img, 0, 0, 0, 0, imagesx( $img ), imagesy( $img ) );
    
            $this->new_extension =  'jpg';
            preg_match( "/.*\/(.*)\.(png)$/i", $params['file'], $path_matches );
            $filename_old = $path_matches[1] . '.' . $path_matches[2];
            $filename_new = $path_matches[1] . '.' . $this->new_extension;
    
    		$i = 0;
    		do{
    			if($i) {
                    $filename_new = $path_matches[1] . '-'. $i . '.' . $this->new_extension;
                }
                $newPath = str_ireplace($filename_old , $filename_new, $params['file'] );
                $i++;
    		}while( file_exists( $newPath ) );
    
    		if( ! file_exists( $newPath ) ){
    			$newUrl = preg_replace( "/\.png$/", "." . $this->new_extension, $params['url'] );
    			if ( imagejpeg( $bg, $newPath, $this->settings['general']['jpg_quality'] ) ){
    				$this->converted_stats = $stats_before - filesize( $newPath );
    				if(
    					! isset( $this->settings['general']['only_lower'] )
    					|| (
    						isset( $this->settings['general']['only_lower'] )
    						&& $this->converted_stats > 0
    					)
    				){
    					if( ! isset( $this->settings['general']['leave_original'] ) ){
    						unlink( $params['file'] );
    					}
    					$params['file'] = $newPath;
    					$params['url'] = $newUrl;
    					$params['type'] = 'image/jpeg';
    					return $params;
    				}else{
    					$this->converted_stats = 0;
    					unlink( $newPath );
    				}
    			}
    		}
    		return 0;
    	}
    
    • This topic was modified 1 year, 10 months ago by payter.
Viewing 1 replies (of 1 total)
  • Plugin Author kubiq

    (@kubiq)

    Hello,

    this was on my todo list 😉

    Unfortunately, it’s not so easy. Thank you for your code, but it works only for newly uploaded images – that’s the easy part.
    But the tricky part is make it work for existing images… imagine image thumbnails used in posts, etc.

    But good news – download version 3.3 – it’s done

Viewing 1 replies (of 1 total)
  • The topic ‘PNG to JPG conversion bug on duplicity extensions’jpe’, ‘jif’, ‘jfi’, ‘jfif’’ is closed to new replies.