本文整理汇总了PHP中wp_get_image_editor函数的典型用法代码示例。如果您正苦于以下问题:PHP wp_get_image_editor函数的具体用法?PHP wp_get_image_editor怎么用?PHP wp_get_image_editor使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了wp_get_image_editor函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: bind
/**
* @param array $post
* @param array $files
*/
public function bind(array $post, array $files = array())
{
parent::bind($post, $files);
// Remove the old image.
if (isset($post['ab_remove_logo']) && file_exists(get_option('ab_settings_company_logo_path'))) {
unlink(get_option('ab_settings_company_logo_path'));
update_option('ab_settings_company_logo_path', '');
update_option('ab_settings_company_logo_url', '');
}
// And add new.
if (isset($files['ab_settings_company_logo']) && $files['ab_settings_company_logo']['tmp_name']) {
if (in_array($files['ab_settings_company_logo']['type'], array("image/gif", "image/jpeg", "image/png"))) {
$uploaded = wp_handle_upload($files['ab_settings_company_logo'], array('test_form' => false));
if ($uploaded) {
$editor = wp_get_image_editor($uploaded['file']);
$editor->resize(200, 200);
$editor->save($uploaded['file']);
$this->data['ab_settings_company_logo_path'] = $uploaded['file'];
$this->data['ab_settings_company_logo_url'] = $uploaded['url'];
// Remove old image.
if (file_exists(get_option('ab_settings_company_logo_path'))) {
unlink(get_option('ab_settings_company_logo_path'));
}
}
}
}
}
示例2: run
/**
* Performs the actual image manipulation,
* including saving the target file.
*
* @param string $load_filename filepath (not URL) to source file
* (ex: /src/var/www/wp-content/uploads/my-pic.jpg)
* @param string $save_filename filepath (not URL) where result file should be saved
* (ex: /src/var/www/wp-content/uploads/my-pic-lbox-300x200-FF3366.jpg)
* @return bool true if everything went fine, false otherwise
*/
public function run($load_filename, $save_filename)
{
$w = $this->w;
$h = $this->h;
$bg = imagecreatetruecolor($w, $h);
$c = self::hexrgb($this->color);
$bgColor = imagecolorallocate($bg, $c['red'], $c['green'], $c['blue']);
imagefill($bg, 0, 0, $bgColor);
$image = wp_get_image_editor($load_filename);
if (!is_wp_error($image)) {
$current_size = $image->get_size();
$quality = $image->get_quality();
$ow = $current_size['width'];
$oh = $current_size['height'];
$new_aspect = $w / $h;
$old_aspect = $ow / $oh;
if ($new_aspect > $old_aspect) {
//taller than goal
$h_scale = $h / $oh;
$owt = $ow * $h_scale;
$y = 0;
$x = $w / 2 - $owt / 2;
$oht = $h;
$image->crop(0, 0, $ow, $oh, $owt, $oht);
} else {
$w_scale = $w / $ow;
$oht = $oh * $w_scale;
$x = 0;
$y = $h / 2 - $oht / 2;
$owt = $w;
$image->crop(0, 0, $ow, $oh, $owt, $oht);
}
$result = $image->save($save_filename);
$func = 'imagecreatefromjpeg';
$save_func = 'imagejpeg';
$ext = pathinfo($save_filename, PATHINFO_EXTENSION);
if ($ext == 'gif') {
$func = 'imagecreatefromgif';
$save_func = 'imagegif';
} else {
if ($ext == 'png') {
$func = 'imagecreatefrompng';
$save_func = 'imagepng';
if ($quality > 9) {
$quality = $quality / 10;
$quality = round(10 - $quality);
}
}
}
$image = $func($save_filename);
imagecopy($bg, $image, $x, $y, 0, 0, $owt, $oht);
if ($save_func === 'imagegif') {
return $save_func($bg, $save_filename);
}
return $save_func($bg, $save_filename, $quality);
} else {
Helper::error_log($image);
}
return false;
}
示例3: wr2x_vt_resize
function wr2x_vt_resize($file_path, $width, $height, $newfile)
{
$orig_size = getimagesize($file_path);
$image_src[0] = $file_path;
$image_src[1] = $orig_size[0];
$image_src[2] = $orig_size[1];
$file_info = pathinfo($file_path);
$extension = '.' . $file_info['extension'];
$no_ext_path = $file_info['dirname'] . '/' . $file_info['filename'];
$cropped_img_path = $no_ext_path . '-' . $width . 'x' . $height . "-tmp" . $extension;
$image = wp_get_image_editor($file_path);
$image->resize($width, $height, true);
$quality = wr2x_getoption("image_quality", "wr2x_advanced", "80");
if (is_numeric($quality)) {
$image->set_quality(intval($quality));
}
$image->save($cropped_img_path);
if (rename($cropped_img_path, $newfile)) {
$cropped_img_path = $newfile;
}
$new_img_size = getimagesize($cropped_img_path);
$new_img = str_replace(basename($image_src[0]), basename($cropped_img_path), $image_src[0]);
$vt_image = array('url' => $new_img, 'width' => $new_img_size[0], 'height' => $new_img_size[1]);
return $vt_image;
}
示例4: cropImageWithFaceDectection
function cropImageWithFaceDectection($metadata, $attachment_id)
{
if (!isset($metadata['sizes'])) {
return $metadata;
}
$upload_path = wp_upload_dir();
$path = $upload_path['basedir'];
//handle the different media upload directory structures
if (isset($path)) {
$file = trailingslashit($upload_path['basedir'] . '/') . $metadata['file'];
} else {
$file = trailingslashit($upload_path['path']) . $metadata['file'];
}
$client = new Client('5e3a3ac24363af113e04a58c61637ea4', 'sXA4iYYphLzg1z8IAcFAtPf8UdcXKwHm', 'http://apicn.faceplusplus.com');
/** @var \FaceCrop\Type\Face[] $result */
$result = $client->detect('http://showbizviet.vn/upload/files/data/2013/8/2/18/466473/1825600192_cham-soc-da-chuan-nhu-ngoc-trinh%202.jpg');
$height = $result[0]->getPosition()->getHeight();
$width = $result[0]->getPosition()->getWidth();
$leftEye = $result[0]->getPosition()->getEyeLeft();
$mouthRight = $result[0]->getPosition()->getMouthRight();
$editor = wp_get_image_editor($file);
$startX = $leftEye->x / $width * 100;
$startY = $leftEye->y / $height * 100;
$editor->crop($startX - 100, $startY - 100, 500, 300, 500, 300, false);
$result = $editor->save($file);
wp_send_json(array($result, $file));
return $metadata;
}
示例5: get_image_url
function get_image_url($path, $id, $width, $height)
{
$image_path = $path;
$upload_directory = wp_upload_dir();
$modified_image_directory = $upload_directory["basedir"] . "/blog/" . $id . "/";
if (!file_exists($modified_image_directory)) {
wp_mkdir_p($modified_image_directory);
}
$file_name_with_ending = explode("/", $image_path);
$file_name_with_ending = $file_name_with_ending[count($file_name_with_ending) - 1];
$file_name_without_ending = explode(".", $file_name_with_ending);
$file_ending = $file_name_without_ending[count($file_name_without_ending) - 1];
$file_name_without_ending = $file_name_without_ending[count($file_name_without_ending) - 2];
$modified_image_path = $modified_image_directory . md5($file_name_without_ending) . "." . $file_ending;
if (!file_exists($modified_image_path)) {
$image = wp_get_image_editor($image_path);
if (!is_wp_error($image)) {
$rotate = 180;
$modified_file_name_without_ending = $file_name_without_ending . "-" . $width . "x" . $height . "-" . $rotate . "dg";
$image->resize($width, $height);
$image->rotate($rotate);
$image->save($modified_file_name_without_ending);
}
}
$modified_image_url = $upload_directory["url"] . "/" . $modified_file_name_without_ending . "." . $file_ending;
return $modified_image_url;
}
示例6: wp_generate_attachment_metadata
public function wp_generate_attachment_metadata($metadata, $attachment_id)
{
$attachment = get_attached_file($attachment_id);
if (!preg_match('!^image/!', get_post_mime_type($attachment_id)) || !file_is_displayable_image($attachment)) {
return $metadata;
}
$image_sizes = Helper::get_image_sizes();
foreach ($image_sizes as $size_name => $size_attributes) {
if (!empty($metadata['sizes'][$size_name])) {
continue;
}
$image = wp_get_image_editor($attachment);
if (is_wp_error($image)) {
continue;
}
$resized = $image->resize($size_attributes['width'], $size_attributes['height'], $size_attributes['crop']);
$image_size = $image->get_size();
if (!is_wp_error($resized) && !empty($image_size)) {
$filename = wp_basename($image->generate_filename());
$extension = pathinfo($filename, PATHINFO_EXTENSION);
$mime_type = $this->get_mime_type($extension);
$metadata['sizes'][$size_name] = array('file' => $filename, 'width' => $image_size['width'], 'height' => $image_size['height'], 'mime-type' => $mime_type);
}
}
return $metadata;
}
示例7: groundup_image_suffix
function groundup_image_suffix($image)
{
// Split the $image path into directory/extension/name
$info = pathinfo($image);
$dir = $info['dirname'] . '/';
$ext = '.' . $info['extension'];
$file_name = wp_basename($image, "{$ext}");
$image_name = substr($file_name, 0, strrpos($file_name, '-'));
// Get image information
$img = wp_get_image_editor($image);
// Get image size, width and height
$img_size = $img->get_size();
// Get new image suffix by comparing image sizes
$image_sizes = get_intermediate_image_sizes();
foreach ($image_sizes as $size) {
$rename = false;
$sizeInfo = get_image_size_data($size);
if ($img_size['width'] == $sizeInfo['width'] && $img_size['height'] <= $sizeInfo['height']) {
$rename = true;
} elseif ($img_size['height'] == $sizeInfo['height'] && $img_size['width'] <= $sizeInfo['width']) {
$rename = true;
}
if ($rename == true) {
// Rename image
$new_name = $dir . $image_name . '-' . $size . $ext;
// Rename the intermediate size
$rename_success = rename($image, $new_name);
if ($rename_success) {
return $new_name;
}
}
}
// do nothing if not renamed
return $image;
}
示例8: __construct
public function __construct($file)
{
$this->setup();
$this->file = $this->sanitize_file_path($file);
$this->editor = wp_get_image_editor($this->source_file());
return $this;
}
示例9: getAttachment
public function getAttachment($attachmentId, $width = null, $height = null, $crop = false, $bookId = false)
{
$width = (int) $width;
$height = (int) $height;
$cachedUrl = $this->getAttachUrlCached($attachmentId, $width, $height, $crop, $bookId);
if ($cachedUrl) {
return $cachedUrl;
}
$attachment = wp_prepare_attachment_for_js($attachmentId);
$filePath = $this->getFilePath($attachment['url']);
// First try:
// Trying to find image in wordpress sizes.
if (!empty($attachment) && $attachment['sizes']) {
foreach ($attachment['sizes'] as $size) {
if ($width && $width === $size['width'] && ($height && $height === $size['height'])) {
$this->saveAttachUrlCached($attachmentId, $width, $height, $crop, $bookId, $size['url']);
return $size['url'];
}
}
}
// Second try
// Trying to find cropped images.
$filename = pathinfo($filePath, PATHINFO_FILENAME);
$filename = $filename . '-' . $width . 'x' . $height . '.' . pathinfo($filePath, PATHINFO_EXTENSION);
if (is_file($file = dirname($filePath) . '/' . $filename)) {
$imgUrl = str_replace(ABSPATH, get_bloginfo('wpurl') . '/', $file);
$this->saveAttachUrlCached($attachmentId, $width, $height, $crop, $bookId, $imgUrl);
return str_replace(ABSPATH, get_bloginfo('wpurl') . '/', $file);
}
// Third and last try
$editor = wp_get_image_editor($filePath);
if (!has_filter('image_resize_dimensions', 'image_crop_dimensions')) {
function image_crop_dimensions($default, $orig_w, $orig_h, $new_w, $new_h, $crop)
{
if (!$crop || !$new_w || !$new_h) {
return null;
}
$size_ratio = max($new_w / $orig_w, $new_h / $orig_h);
$crop_w = round($new_w / $size_ratio);
$crop_h = round($new_h / $size_ratio);
$s_x = floor(($orig_w - $crop_w) / 2);
$s_y = floor(($orig_h - $crop_h) / 2);
return array(0, 0, (int) $s_x, (int) $s_y, (int) $new_w, (int) $new_h, (int) $crop_w, (int) $crop_h);
}
add_filter('image_resize_dimensions', 'image_crop_dimensions', 10, 6);
}
if (is_wp_error($editor) || is_wp_error($editor->resize($width, $height, (bool) $crop))) {
$imgUrl = isset($attachment['sizes'], $attachment['sizes']['full'], $attachment['sizes']['full']['url']) ? $attachment['sizes']['full']['url'] : $attachment['url'];
$this->saveAttachUrlCached($attachmentId, $width, $height, $crop, $bookId, $imgUrl);
return $imgUrl;
}
if (is_wp_error($data = $editor->save())) {
return $attachment['sizes']['full']['url'];
}
$editor = null;
unset($editor);
$imgUrl = str_replace(ABSPATH, get_bloginfo('wpurl') . '/', $data['path']);
$this->saveAttachUrlCached($attachmentId, $width, $height, $crop, $bookId, $imgUrl);
return str_replace(ABSPATH, get_bloginfo('wpurl') . '/', $data['path']);
}
示例10: run
/**
* Performs the actual image manipulation,
* including saving the target file.
*
* @param string $load_filename filepath (not URL) to source file
* (ex: /src/var/www/wp-content/uploads/my-pic.jpg)
* @param string $save_filename filepath (not URL) where result file should be saved
* (ex: /src/var/www/wp-content/uploads/my-pic@2x.jpg)
* @return bool true if everything went fine, false otherwise
*/
function run($load_filename, $save_filename)
{
$image = wp_get_image_editor($load_filename);
if (!is_wp_error($image)) {
$current_size = $image->get_size();
$src_w = $current_size['width'];
$src_h = $current_size['height'];
// Get ratios
$w = $src_w * $this->factor;
$h = $src_h * $this->factor;
$image->crop(0, 0, $src_w, $src_h, $w, $h);
$result = $image->save($save_filename);
if (is_wp_error($result)) {
error_log('Error resizing image');
error_log(print_r($result, true));
return false;
} else {
return true;
}
} else {
if (isset($image->error_data['error_loading_image'])) {
TimberHelper::error_log('Error loading ' . $image->error_data['error_loading_image']);
} else {
TimberHelper::error_log($image);
}
}
return false;
}
示例11: hack_image_make_intermediate_size
function hack_image_make_intermediate_size($file, $width, $height, $crop = false, $size = "")
{
if ($width || $height) {
if ($size == "thumbnail" || $size == "medium" || $size == "large") {
$suffix = $size;
} else {
global $_wp_additional_image_sizes;
if (isset($_wp_additional_image_sizes[$size])) {
$suffix = $size;
} else {
$suffix = null;
}
}
//コアファイルを触らずにサムネイル(jpg)のクオリティ値を変えられます。デフォルトは90。
$image = wp_get_image_editor($file);
// Return an implementation that extends <tt>WP_Image_Editor</tt>
if (!is_wp_error($image)) {
if (empty($suffix)) {
$suffix = "{$width}x{$height}";
}
$pathinfo = pathinfo($file);
$dir = $pathinfo['dirname'];
$ext = $pathinfo['extension'];
$name = basename($file, ".{$ext}");
$resized_file = "{$dir}/{$name}-{$suffix}.{$ext}";
$image->resize($width, $height, $crop);
$image->save($resized_file);
if ($info = getimagesize($resized_file)) {
$resized_file = apply_filters('image_make_intermediate_size', $resized_file);
return array('file' => wp_basename($resized_file), 'width' => $info[0], 'height' => $info[1], 'size' => $size);
}
}
}
return false;
}
示例12: run
/**
* Performs the actual image manipulation,
* including saving the target file.
*
* @param string $load_filename filepath (not URL) to source file
* (ex: /src/var/www/wp-content/uploads/my-pic.jpg)
* @param string $save_filename filepath (not URL) where result file should be saved
* (ex: /src/var/www/wp-content/uploads/my-pic@2x.jpg)
* @return bool true if everything went fine, false otherwise
*/
public function run($load_filename, $save_filename)
{
$image = wp_get_image_editor($load_filename);
if (!is_wp_error($image)) {
$current_size = $image->get_size();
$src_w = $current_size['width'];
$src_h = $current_size['height'];
// Get ratios
$w = $src_w * $this->factor;
$h = $src_h * $this->factor;
$image->crop(0, 0, $src_w, $src_h, $w, $h);
$result = $image->save($save_filename);
if (is_wp_error($result)) {
// @codeCoverageIgnoreStart
Helper::error_log('Error resizing image');
Helper::error_log($result);
return false;
// @codeCoverageIgnoreEnd
}
return true;
} else {
if (isset($image->error_data['error_loading_image'])) {
Helper::error_log('Error loading ' . $image->error_data['error_loading_image']);
return false;
}
}
Helper::error_log($image);
return false;
}
示例13: initImageParams
/**
*
* init image parameters from url
*/
private function initImageParams($urlImage, $size = UniteFunctionsWPBiz::THUMB_MEDIUM, $ratio = 'none', $refresh = 'false')
{
if (is_numeric($urlImage)) {
$this->thumbID = $urlImage;
$this->imageUrl = UniteFunctionsWPBiz::getUrlAttachmentImage($this->thumbID, $size);
$this->imageUrlOrig = UniteFunctionsWPBiz::getUrlAttachmentImage($this->thumbID, 'full');
} else {
$this->imageUrl = $urlImage;
$this->imageUrlOrig = $urlImage;
}
//set image path, file and url
if (!empty($this->imageUrl)) {
$this->imageFilepath = UniteFunctionsWPBiz::getImagePathFromURL($this->imageUrl);
$realPath = UniteFunctionsWPBiz::getPathUploads() . $this->imageFilepath;
//scale img if needed
if ($ratio !== 'none') {
$ratio = explode('_', $ratio);
if (count($ratio) == 2) {
$image = wp_get_image_editor($realPath);
if (!is_wp_error($image)) {
$origSize = $image->get_size();
if (isset($origSize['width']) && $origSize['width'] > 0 && isset($origSize['height']) && $origSize['height'] > 0) {
$doCreate = true;
//get new dimensions based on the scale ratio
$newSize = UniteFunctionsBiz::getImageSizeByRatio($origSize['width'], $origSize['height'], $ratio['0'], $ratio['1']);
//check if file exists with dimensions
$suffix = $image->get_suffix();
$fnCheck = $image->generate_filename();
$fnCheck = str_replace($suffix . '.', $newSize['0'] . 'x' . $newSize['1'] . '.', $fnCheck);
//check if file exists
if (file_exists($fnCheck) != false) {
if ($refresh == 'false') {
$doCreate = false;
}
}
//refresh
if ($doCreate) {
$image->resize($newSize['0'], $newSize['1'], true);
$newImage = $image->generate_filename();
$image->save($newImage);
} else {
$newImage = $fnCheck;
}
if (trim($newImage) !== '') {
$this->imageUrl = str_replace(ABSPATH, '', $newImage);
$this->imageFilepath = UniteFunctionsWPBiz::getImagePathFromURL($this->imageUrl);
$realPath = UniteFunctionsWPBiz::getPathUploads() . $this->imageFilepath;
$this->imageUrl = get_bloginfo('wpurl') . '/' . $this->imageUrl;
}
}
}
}
}
if (file_exists($realPath) == false || is_file($realPath) == false) {
$this->imageFilepath = "";
}
$this->imageFilename = basename($this->imageUrl);
}
}
示例14: image_resize35
/**
* This function is almost equal to the image_resize (native function of wordpress)
*/
function image_resize35($file, $max_w, $max_h, $crop = false, $far = false, $iar = false, $dest_path = null, $jpeg_quality = 90)
{
$image = wp_get_image_editor($file);
if (is_wp_error($image)) {
return $image;
}
$size = @getimagesize($file);
if (!$size) {
return new WP_Error('invalid_image', __('Could not read image size'), $file);
}
list($orig_w, $orig_h, $orig_type) = $size;
$dims = mf_image_resize_dimensions($orig_w, $orig_h, $max_w, $max_h, $crop, $far, $iar);
if (!$dims) {
$dims = array(0, 0, 0, 0, $orig_w, $orig_h, $orig_w, $orig_h);
}
list($dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h) = $dims;
$newimage = imagecreatetruecolor($dst_w, $dst_h);
imagealphablending($newimage, false);
imagesavealpha($newimage, true);
$transparent = imagecolorallocatealpha($newimage, 255, 255, 255, 127);
imagefilledrectangle($newimage, 0, 0, $dst_w, $dst_h, $transparent);
@ini_set('memory_limit', apply_filters('image_memory_limit', WP_MAX_MEMORY_LIMIT));
$imageTmp = imagecreatefromstring(file_get_contents($file));
imagecopyresampled($newimage, $imageTmp, $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h);
// convert from full colors to index colors, like original PNG.
if (IMAGETYPE_PNG == $orig_type && !imageistruecolor($imageTmp)) {
imagetruecolortopalette($newimage, false, imagecolorstotal($imageTmp));
}
// we don't need the original in memory anymore
imagedestroy($imageTmp);
$info = pathinfo($dest_path);
$dir = $info['dirname'];
$ext = $info['extension'];
$name = basename($dest_path, ".{$ext}");
$destfilename = "{$dir}/{$name}.{$ext}";
if (IMAGETYPE_GIF == $orig_type) {
if (!imagegif($newimage, $destfilename)) {
return new WP_Error('resize_path_invalid', __('Resize path invalid'));
}
} elseif (IMAGETYPE_PNG == $orig_type) {
if (!imagepng($newimage, $destfilename)) {
return new WP_Error('resize_path_invalid', __('Resize path invalid'));
}
} else {
// all other formats are converted to jpg
//Todo: add option for use progresive JPG
//imageinterlace($newimage, true); //Progressive JPG
if (!imagejpeg($newimage, $destfilename, apply_filters('jpeg_quality', $jpeg_quality, 'image_resize'))) {
return new WP_Error('resize_path_invalid', __('Resize path invalid'));
}
}
imagedestroy($newimage);
// Set correct file permissions
$stat = stat(dirname($destfilename));
$perms = $stat['mode'] & 0666;
//same permissions as parent folder, strip off the executable bits
@chmod($destfilename, $perms);
return $destfilename;
}
示例15: convert_image
/**
* Convert image to a new mime type.
*
* @since 1.0
*
* @param string The filepath of an image
* @param string The new mime type of the image. By default jpg
* @return array|WP_Error {'path'=>string, 'file'=>string, 'width'=>int, 'height'=>int, 'mime-type'=>string}
*/
public static function convert_image($filepath, $new_mime_type = null)
{
if (!$new_mime_type) {
$new_mime_type = self::$new_type;
}
$editor = wp_get_image_editor($filepath);
return $editor->save($filepath, $new_mime_type);
}