Page 1 of 1

Just if resized image exist

Posted: 2014-07-14T04:36:07-07:00
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

Re: Just if resized image exist

Posted: 2014-07-14T05:01:55-07:00
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.

Re: Just if resized image exist

Posted: 2014-07-14T05:13:08-07:00
by levipadre
Ok, but really slow the loading time.
Do you have any other idea?

Re: Just if resized image exist

Posted: 2014-07-14T05:16:59-07:00
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

Re: Just if resized image exist

Posted: 2014-07-14T05:19:05-07:00
by levipadre
Ok, thank you for your time.