Just if resized image exist

Questions and postings pertaining to the development of ImageMagick, feature enhancements, and ImageMagick internals. ImageMagick source code and algorithms are discussed here. Usage questions which are too arcane for the normal user list should also be posted here.
Post Reply
levipadre
Posts: 3
Joined: 2014-07-14T04:27:35-07:00
Authentication code: 6789

Just if resized image exist

Post by levipadre »

Hi everybody,

I have a question.
I implemented a gallery using ImageMagick to WordPress, but seems slow. I tried to optimize but nothing.
Is there any chance to run the resize process just that time if the resized image is doesn't exist?
My current code is:

Code: Select all

<?php 
        $imgdata = wp_get_attachment_image_src( get_post_thumbnail_id(), 'large' );
        $imgurl = $imgdata[0]; // the url of the thumbnail picture
        $imgwidth = $imgdata[1]; // thumbnail's width
        $imgheight = $imgdata[2]; // thumbnail's height

		
		if($imgurl != '') {
      
		$parsed_url = parse_url($imgurl);
		$path = $_SERVER['DOCUMENT_ROOT'] . $parsed_url['path'];
		
		$img = wp_get_image_editor( $path ); 		
	
			if ( ! is_wp_error( $img ) ) { 
					$old_size = $img->get_size();
					
					$img->resize( 140, 140, true );
					$suffix = $img->get_suffix();
					$filename = $img->generate_filename( 'resized-to-' . $suffix , $parsed_url['path'] , NULL );
					if ($resize !== FALSE) { 
						$new_size = $img->get_size();
						$img->set_quality(80);
						$saved = $img->save($filename,NULL);
					}
			}
	
			$newpath = str_replace('C:/wamp/www/', '/', $filename);
			
		   ?>
		  
		  <img src="<?php echo $newpath; ?>" width="<?php echo $saved['width']; ?>" height="<?php echo $saved['height']; ?>" alt="" />
		  
			
		<?php } ?>
Thanks,
levipadre
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: Just if resized image exist

Post by Bonzo »

I would suggest you need to check if the image exists and then decide if to run the resize code or not; I do not think Imagemagick can make that decision.
This would be best done with php in your case.
levipadre
Posts: 3
Joined: 2014-07-14T04:27:35-07:00
Authentication code: 6789

Re: Just if resized image exist

Post by levipadre »

Ok, but really slow the loading time.
Do you have any other idea?
Bonzo
Posts: 2971
Joined: 2006-05-20T08:08:19-07:00
Location: Cambridge, England

Re: Just if resized image exist

Post by Bonzo »

Do you have any other idea?
It is hard to say as the code you posted does not show any Imagemagick code and so I do not know what defaults etc. the wordpress program is using.

The simple alternative is resize on your PC and only Upload that image - if that is possible with Wordpress
levipadre
Posts: 3
Joined: 2014-07-14T04:27:35-07:00
Authentication code: 6789

Re: Just if resized image exist

Post by levipadre »

Ok, thank you for your time.
Post Reply