Skip to content

Instantly share code, notes, and snippets.

@kurtpayne
Created September 6, 2012 18:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kurtpayne/3659197 to your computer and use it in GitHub Desktop.
Save kurtpayne/3659197 to your computer and use it in GitHub Desktop.
Abstract that thing
<?php
abstract class WP_Image_Editor_Base {
protected $file = false;
protected $size = false;
protected $orig_type = false;
protected $quality = 90;
public function __construct( $filename ) {
xdebug_start_trace();
$this->file = $filename;
}
public function __destruct() {
xdebug_stop_trace();
}
public static function test() {
return false;
}
abstract protected function load();
abstract public function resize( $max_w, $max_h, $crop = false );
abstract public function multi_resize( $sizes );
abstract public function crop( $src_x, $src_y, $src_w, $src_h, $dst_w = null, $dst_h = null, $src_abs = false );
abstract public function rotate( $angle );
abstract public function flip( $horz, $vert );
abstract public function save( $destfilename = null );
abstract public function stream();
public function get_size() {
if ( ! $this->load() )
return;
return $this->size;
}
protected function update_size( $width = false, $height = false ) {
$this->size = array(
'width' => $width,
'height' => $height
);
}
public function set_quality( $quality ) {
$this->quality = apply_filters( 'wp_editor_set_quality', $quality );
}
public function generate_filename( $suffix = null, $dest_path = null ) {
if ( ! $this->load() )
return;
// $suffix will be appended to the destination filename, just before the extension
$suffix = $this->get_suffix();
$info = pathinfo( $this->file );
$dir = $info['dirname'];
$ext = $info['extension'];
$name = wp_basename( $this->file, ".$ext" );
if ( ! is_null( $dest_path ) && $_dest_path = realpath( $dest_path ) )
$dir = $_dest_path;
return "{$dir}/{$name}-{$suffix}.{$ext}";
}
public function get_suffix() {
if ( ! $this->get_size() )
return;
if ( ! $this->load() )
return;
return "{$this->size['width']}x{$this->size['height']}";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment