Tagged with " image"
Aug 25, 2014 - WordPress    Comments Off on Getting an attachment from a URL

Getting an attachment from a URL

Specifically, to get a different size of the image.

        $attachment_id = get_attachment_id_from_src($solutions_metabox->get_the_value('image'));
        $image_url = wp_get_attachment_image_src($attachment_id,'solutions');
Jul 18, 2013 - WordPress    Comments Off on Combo image function

Combo image function

I created this to stack two images horizontally together, to make before and after images into one image. Designed for WordPress, but could be tweaked for other systems.

/**
 * use two images to create one image
 */
function msdlab_combine_images($params){
	extract(
		array_merge(
			array(
				'images' => array(),
				'orientation' => 'horizontal',
				'output_format' => 'jpg'
			)
		,$params)
	);
	//get info on each image
	foreach($images AS $key => $url){
		$imgs[$key]['url'] = $url;
		$imgs[$key]['path'] = $path = realpath(preg_replace('@'.site_url().'@i','.',$url));
		$imgs[$key]['file'] = $file = basename($path);
		$imgs[$key]['filename'] = $filename = preg_replace('/\.(?:jpg|jpeg|bmp|png|gif)/i','',$file);
		$imgs[$key]['info'] = $info = getimagesize($path);
		$comboname .= $filename;
		switch($orientation){
			case 'horizontal':
				$combowidth = $combowidth + $info[0];
				$comboheight = $info[1]>$comboheight?$info[1]:$comboheight;
				break;
			case 'vertical':
				$combowidth = $info[0]>$combowidth?$info[0]:$combowidth;
				$comboheight = $comboheight + $info[1];
				break;
		}
	}
	$comboname .= '.'.$output_format;
	$upload_dir = wp_upload_dir();
	//check if it exists
	if(file_exists($upload_dir['basedir'].'/'.$comboname)){
		return $upload_dir['baseurl'].'/'.$comboname;
	}
	//create a holding image
	$newimg = @imagecreatetruecolor($combowidth, $comboheight);
	foreach($imgs AS $key => $img){
		switch($img['info']['mime']){
			case 'image/jpeg':
				$src = @imagecreatefromjpeg($img['path']);
				break;
			case 'image/png':
				$src = @imagecreatefrompng($img['path']);
				break;
			case 'image/gif':
				$src = @imagecreatefromgif($img['path']);
				break;
			default:
				return 'unsupported file type';
		}
		//copy things
		switch($orientation){
			case 'horizontal':
				imagecopy($newimg, $src, $imgs[$key-1]['info'][0], 0, 0, 0, $img['info'][0], $img['info'][1]);
				break;
			case 'vertical':
				imagecopy($newimg, $src, 0, $imgs[$key-1]['info'][1], 0, 0, $img['info'][0], $img['info'][1]);
				break;
		}
		// free up memory
		imagedestroy($src);
	}
	// Save the image
	imagejpeg($newimg, $upload_dir['basedir'].'/'.$comboname);

	// Free up memory
	imagedestroy($newimg);

	//return the image
	return $upload_dir['baseurl'].'/'.$comboname;
}
Dec 26, 2011 - WordPress    Comments Off on Default to no link on images

Default to no link on images

This goes into the functions.php file or similar:

//remove auto-links on media
update_option('image_default_link_type','none');
Dec 26, 2011 - Genesis    Comments Off on Genesis Post Image

Genesis Post Image

Default only works on certain pages/posts:

add_action('genesis_post_content', 'genesis_do_post_image');
/**
* Post Image
*/
function genesis_do_post_image() {
if ( !is_singular() && genesis_get_option('content_archive_thumbnail') ) {
$img = genesis_get_image( array( 'format' => 'html', 'size' => genesis_get_option('image_size'), 'attr' => array( 'class' => 'alignleft post-image' ) ) );
printf( '%s', get_permalink(), the_title_attribute('echo=0'), $img );
}

}

Here’s a sample fix:

remove_action( 'genesis_post_content','genesis_do_post_image');
add_action( 'genesis_before_post_content','tapestry_do_post_image');</code>

function tapestry_do_post_image() {
$img = genesis_get_image( array( 'format' =&gt; 'html', 'size' =&gt; genesis_get_option('image_size'), 'attr' =&gt; array( 'class' =&gt; 'aligncenter post-image' ) ) );
printf( '<a title="%s" href="%s">%s</a>
<div class="clear"></div>
', get_permalink(), the_title_attribute('echo=0'), $img );
}