本文整理汇总了PHP中wp_imagecreatetruecolor函数的典型用法代码示例。如果您正苦于以下问题:PHP wp_imagecreatetruecolor函数的具体用法?PHP wp_imagecreatetruecolor怎么用?PHP wp_imagecreatetruecolor使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了wp_imagecreatetruecolor函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: fit
/**
* Fit small image to specified bound
*
* @param string $src
* @param string $dest
* @param int $width
* @param int $height
* @return bool
*/
public function fit($src, $dest, $width, $height)
{
// Calculate
$size = getimagesize($src);
$ratio = max($width / $size[0], $height / $size[1]);
$old_width = $size[0];
$old_height = $size[1];
$new_width = intval($old_width * $ratio);
$new_height = intval($old_height * $ratio);
// Resize
@ini_set('memory_limit', apply_filters('image_memory_limit', WP_MAX_MEMORY_LIMIT));
$image = imagecreatefromstring(file_get_contents($src));
$new_image = wp_imagecreatetruecolor($new_width, $new_height);
imagecopyresampled($new_image, $image, 0, 0, 0, 0, $new_width, $new_height, $old_width, $old_height);
if (IMAGETYPE_PNG == $size[2] && function_exists('imageistruecolor') && !imageistruecolor($image)) {
imagetruecolortopalette($new_image, false, imagecolorstotal($image));
}
// Destroy old image
imagedestroy($image);
// Save
switch ($size[2]) {
case IMAGETYPE_GIF:
$result = imagegif($new_image, $dest);
break;
case IMAGETYPE_PNG:
$result = imagepng($new_image, $dest);
break;
default:
$result = imagejpeg($new_image, $dest);
break;
}
imagedestroy($new_image);
return $result;
}
示例2: _resize_padded
protected function _resize_padded($canvas_w, $canvas_h, $canvas_color, $width, $height, $orig_w, $orig_h, $origin_x, $origin_y)
{
$src_x = $src_y = 0;
$src_w = $orig_w;
$src_h = $orig_h;
$cmp_x = $orig_w / $width;
$cmp_y = $orig_h / $height;
// calculate x or y coordinate and width or height of source
if ($cmp_x > $cmp_y) {
$src_w = round($orig_w / $cmp_x * $cmp_y);
$src_x = round(($orig_w - $orig_w / $cmp_x * $cmp_y) / 2);
} else {
if ($cmp_y > $cmp_x) {
$src_h = round($orig_h / $cmp_y * $cmp_x);
$src_y = round(($orig_h - $orig_h / $cmp_y * $cmp_x) / 2);
}
}
$resized = wp_imagecreatetruecolor($canvas_w, $canvas_h);
if ($canvas_color === 'transparent') {
$color = imagecolorallocatealpha($resized, 255, 255, 255, 127);
} else {
$rgb = cnColor::rgb2hex2rgb($canvas_color);
$color = imagecolorallocatealpha($resized, $rgb['red'], $rgb['green'], $rgb['blue'], 0);
}
// Fill the background of the new image with allocated color.
imagefill($resized, 0, 0, $color);
// Restore transparency.
imagesavealpha($resized, TRUE);
imagecopyresampled($resized, $this->image, $origin_x, $origin_y, $src_x, $src_y, $width, $height, $src_w, $src_h);
if (is_resource($resized)) {
$this->update_size($width, $height);
return $resized;
}
return new WP_Error('image_resize_error', __('Image resize failed.', 'connections'), $this->file);
}
示例3: wp_crop_image
/**
* Crop an Image to a given size.
*
* @since 2.1.0
*
* @param string|int $src_file The source file or Attachment ID.
* @param int $src_x The start x position to crop from.
* @param int $src_y The start y position to crop from.
* @param int $src_w The width to crop.
* @param int $src_h The height to crop.
* @param int $dst_w The destination width.
* @param int $dst_h The destination height.
* @param int $src_abs Optional. If the source crop points are absolute.
* @param string $dst_file Optional. The destination file to write to.
* @return string New filepath on success, String error message on failure.
*/
function wp_crop_image($src_file, $src_x, $src_y, $src_w, $src_h, $dst_w, $dst_h, $src_abs = false, $dst_file = false)
{
if (is_numeric($src_file)) {
// Handle int as attachment ID
$src_file = get_attached_file($src_file);
}
$src = wp_load_image($src_file);
if (!is_resource($src)) {
return $src;
}
$dst = wp_imagecreatetruecolor($dst_w, $dst_h);
if ($src_abs) {
$src_w -= $src_x;
$src_h -= $src_y;
}
if (function_exists('imageantialias')) {
imageantialias($dst, true);
}
imagecopyresampled($dst, $src, 0, 0, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h);
imagedestroy($src);
// Free up memory
if (!$dst_file) {
$dst_file = str_replace(basename($src_file), 'cropped-' . basename($src_file), $src_file);
}
$dst_file = preg_replace('/\\.[^\\.]+$/', '.jpg', $dst_file);
if (imagejpeg($dst, $dst_file, apply_filters('jpeg_quality', 90, 'wp_crop_image'))) {
return $dst_file;
} else {
return false;
}
}
示例4: wp_crop_image
/**
* Crop an Image to a given size.
*
* @since 2.1.0
*
* @param string|int $src The source file or Attachment ID.
* @param int $src_x The start x position to crop from.
* @param int $src_y The start y position to crop from.
* @param int $src_w The width to crop.
* @param int $src_h The height to crop.
* @param int $dst_w The destination width.
* @param int $dst_h The destination height.
* @param int $src_abs Optional. If the source crop points are absolute.
* @param string $dst_file Optional. The destination file to write to.
* @return string|WP_Error|false New filepath on success, WP_Error or false on failure.
*/
function wp_crop_image($src, $src_x, $src_y, $src_w, $src_h, $dst_w, $dst_h, $src_abs = false, $dst_file = false)
{
if (0 == $src_x && 0 == $src_y && $src_w == $dst_w && $src_h == $dst_h) {
return is_numeric($src) ? get_attached_file($src) : $src;
}
if (is_numeric($src)) {
// Handle int as attachment ID
$src_file = get_attached_file($src);
if (!file_exists($src_file)) {
// If the file doesn't exist, attempt a url fopen on the src link.
// This can occur with certain file replication plugins.
$post = get_post($src);
$image_type = $post->post_mime_type;
$src = load_image_to_edit($src, $post->post_mime_type, 'full');
} else {
$size = @getimagesize($src_file);
$image_type = $size ? $size['mime'] : '';
$src = wp_load_image($src_file);
}
} else {
$size = @getimagesize($src);
$image_type = $size ? $size['mime'] : '';
$src = wp_load_image($src);
}
if (!is_resource($src)) {
return new WP_Error('error_loading_image', $src, $src_file);
}
$dst = wp_imagecreatetruecolor($dst_w, $dst_h);
if ($src_abs) {
$src_w -= $src_x;
$src_h -= $src_y;
}
if (function_exists('imageantialias')) {
imageantialias($dst, true);
}
imagecopyresampled($dst, $src, 0, 0, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h);
imagedestroy($src);
// Free up memory
if (!$dst_file) {
$dst_file = str_replace(basename($src_file), 'cropped-' . basename($src_file), $src_file);
}
if ('image/png' != $image_type) {
$dst_file = preg_replace('/\\.[^\\.]+$/', '.jpg', $dst_file);
}
// The directory containing the original file may no longer exist when
// using a replication plugin.
wp_mkdir_p(dirname($dst_file));
if ('image/png' == $image_type && imagepng($dst, $dst_file)) {
return $dst_file;
} elseif (imagejpeg($dst, $dst_file, apply_filters('jpeg_quality', 90, 'wp_crop_image'))) {
return $dst_file;
} else {
return false;
}
}
示例5: crop
/**
* Crops Image.
*
* @since 3.5.0
* @access public
*
* @param string|int $src The source file or Attachment ID.
* @param int $src_x The start x position to crop from.
* @param int $src_y The start y position to crop from.
* @param int $src_w The width to crop.
* @param int $src_h The height to crop.
* @param int $dst_w Optional. The destination width.
* @param int $dst_h Optional. The destination height.
* @param boolean $src_abs Optional. If the source crop points are absolute.
* @return boolean|WP_Error
*/
public function crop($src_x, $src_y, $src_w, $src_h, $dst_w = null, $dst_h = null, $src_abs = false)
{
$ar = $src_w / $src_h;
$dst_ar = $dst_w / $dst_h;
if (isset($_GET['pte-fit-crop-color']) && abs($ar - $dst_ar) > 0.01) {
PteLogger::debug(sprintf("AR: '%f'\tOAR: '%f'", $ar, $dst_ar));
// Crop the image to the correct aspect ratio...
if ($dst_ar > $ar) {
// constrain to the dst_h
$tmp_dst_h = $dst_h;
$tmp_dst_w = $dst_h * $ar;
$tmp_dst_y = 0;
$tmp_dst_x = $dst_w / 2 - $tmp_dst_w / 2;
} else {
$tmp_dst_w = $dst_w;
$tmp_dst_h = $dst_w / $ar;
$tmp_dst_x = 0;
$tmp_dst_y = $dst_h / 2 - $tmp_dst_h / 2;
}
// copy $this->image unto a new image with the right width/height.
$img = wp_imagecreatetruecolor($dst_w, $dst_h);
if (function_exists('imageantialias')) {
imageantialias($img, true);
}
if (preg_match("/^#[a-fA-F0-9]{6}\$/", $_GET['pte-fit-crop-color'])) {
$c = self::getRgbFromHex($_GET['pte-fit-crop-color']);
$color = imagecolorallocate($img, $c[0], $c[1], $c[2]);
} else {
PteLogger::debug("setting transparent/white");
//$color = imagecolorallocate( $img, 100, 100, 100 );
$color = imagecolorallocatealpha($img, 255, 255, 255, 127);
}
imagefilledrectangle($img, 0, 0, $dst_w, $dst_h, $color);
imagecopyresampled($img, $this->image, $tmp_dst_x, $tmp_dst_y, $src_x, $src_y, $tmp_dst_w, $tmp_dst_h, $src_w, $src_h);
if (is_resource($img)) {
imagedestroy($this->image);
$this->image = $img;
$this->update_size();
return true;
}
return new WP_Error('image_crop_error', __('Image crop failed.'), $this->file);
}
return parent::crop($src_x, $src_y, $src_w, $src_h, $dst_w, $dst_h, $src_abs);
}
示例6: catch_upload
/**
* Catch the file upload and parse it
*
* @since 0.1
*
* @param int $post_id
* @return string The filepath
*/
public static function catch_upload($post_id)
{
if (!wp_attachment_is_image($post_id)) {
return;
}
// Get default options for Large Size Media and options
$max_width = (int) get_option('large_size_w', 1024);
$max_height = (int) get_option('large_size_h', 1024);
$crop = (bool) get_option('media_features_crop', false);
$resizes = (bool) get_option('media_features_resize', false);
// If option is not enabled return
if (!$resizes) {
return;
}
// Let's see if these sizes are reasonable
if ($max_width < 0 || $max_height < 0) {
return;
}
// Get file and image GD resource
$file = get_attached_file($post_id);
$image = wp_load_image($file);
if (!is_resource($image)) {
return new WP_Error('error_loading_image', $image, $file);
}
// Get real dimensions from file image
list($orig_width, $orig_height, $orig_type) = @getimagesize($file);
// if the image needs to be cropped, resize it
if ($max_width >= $orig_width && $max_height >= $orig_height) {
return;
}
//var_dump( $file, $orig_width, $orig_height, $orig_type, $max_width, $max_height );
$dims = image_resize_dimensions($orig_width, $orig_height, $max_width, $max_height, $crop);
if (!$dims) {
return new WP_Error('error_getting_dimensions', __('Could not calculate resized image dimensions', 'media-features'));
}
list($dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h) = $dims;
$newimage = wp_imagecreatetruecolor($dst_w, $dst_h);
imagecopyresampled($newimage, $image, $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 && function_exists('imageistruecolor') && !imageistruecolor($image)) {
imagetruecolortopalette($newimage, false, imagecolorstotal($image));
}
// we don't need the original in memory anymore
imagedestroy($image);
extract(pathinfo($file));
$name = wp_basename($file, ".{$extension}");
//var_dump( $name, $dirname, $extension );
$dirname = realpath($dirname);
$dest_file = "{$dirname}/{$name}.{$extension}";
// If the file is not writable should not proceed
if (!is_writeable($dest_file)) {
return;
}
// Save to file based on original type
$did_save = false;
switch ($orig_type) {
case IMAGETYPE_GIF:
imagegif($newimage, $dest_file);
break;
case IMAGETYPE_PNG:
imagepng($newimage, $dest_file);
break;
case IMAGETYPE_JPEG:
case IMAGETYPE_JPEG2000:
imagejpeg($newimage, $dest_file, apply_filters('jpeg_quality', 90, 'image_resize'));
break;
default:
$dest_file = "{$dirname}/{$name}.{$extension}";
}
imagedestroy($newimage);
// Set correct file permissions
$stat = stat(dirname($dest_file));
$perms = $stat['mode'] & 0666;
//same permissions as parent folder, strip off the executable bits
@chmod($dest_file, $perms);
return $dest_file;
}
示例7: wp_save_image
function wp_save_image($post_id)
{
$msg = '';
$success = $delete = $full_resized = false;
$post = get_post($post_id);
@ini_set('memory_limit', '256M');
$img = load_image_to_edit($post);
if (!is_resource($img)) {
return 'error=' . __('Unable to create new image.');
}
$fwidth = !empty($_REQUEST['fwidth']) ? intval($_REQUEST['fwidth']) : 0;
$fheight = !empty($_REQUEST['fheight']) ? intval($_REQUEST['fheight']) : 0;
$target = !empty($_REQUEST['target']) ? preg_replace('/[^a-z0-9_-]+/i', '', $_REQUEST['target']) : '';
if (!empty($_REQUEST['history'])) {
$changes = json_decode(stripslashes($_REQUEST['history']));
if ($changes) {
$img = image_edit_apply_changes($img, $changes);
}
}
if ($fwidth > 0 && $fheight > 0) {
// scale the full size image
$dst = wp_imagecreatetruecolor($fwidth, $fheight);
if (imagecopyresampled($dst, $img, 0, 0, 0, 0, $fwidth, $fheight, imagesx($img), imagesy($img))) {
imagedestroy($img);
$img = $dst;
$full_resized = true;
}
}
if (!$changes && !$full_resized) {
return 'error=' . __('Nothing to save, the image is not changed.');
}
$meta = wp_get_attachment_metadata($post_id, false, false);
if (!is_array($meta)) {
$meta = array();
}
if (!isset($meta['sizes']) || !is_array($meta['sizes'])) {
$meta['sizes'] = array();
}
// generate new filename
$path = get_attached_file($post_id);
$path_parts = pathinfo52($path);
$filename = $path_parts['filename'];
$suffix = time() . rand(100, 999);
while (true) {
$filename = preg_replace('/-e([0-9]+)$/', '', $filename);
$filename .= "-e{$suffix}";
$new_filename = "{$filename}.{$path_parts['extension']}";
$new_path = "{$path_parts['dirname']}/{$new_filename}";
if (file_exists($new_path)) {
$suffix++;
} else {
break;
}
}
// save the full-size file, also needed to create sub-sizes
if (!wp_save_image_file($new_path, $img, $post->post_mime_type, $post_id)) {
return 'error=' . __('Unable to save the image.');
}
if ('full' == $target || 'all' == $target || $full_resized) {
$meta['sizes']["backup-{$suffix}-full"] = array('width' => $meta['width'], 'height' => $meta['height'], 'file' => $path_parts['basename']);
$success = update_attached_file($post_id, $new_path);
$meta['file'] = get_attached_file($post_id, true);
// get the path unfiltered
$meta['width'] = imagesx($img);
$meta['height'] = imagesy($img);
list($uwidth, $uheight) = wp_shrink_dimensions($meta['width'], $meta['height']);
$meta['hwstring_small'] = "height='{$uheight}' width='{$uwidth}'";
if ($success && $target == 'all') {
$sizes = apply_filters('intermediate_image_sizes', array('large', 'medium', 'thumbnail'));
}
$msg .= "full={$meta['width']}x{$meta['height']}!";
} elseif (array_key_exists($target, $meta['sizes'])) {
$sizes = array($target);
$success = $delete = true;
}
if (isset($sizes)) {
foreach ($sizes as $size) {
if (isset($meta['sizes'][$size])) {
$meta['sizes']["backup-{$suffix}-{$size}"] = $meta['sizes'][$size];
}
$resized = image_make_intermediate_size($new_path, get_option("{$size}_size_w"), get_option("{$size}_size_h"), get_option("{$size}_crop"));
if ($resized) {
$meta['sizes'][$size] = $resized;
} else {
unset($meta['sizes'][$size]);
}
}
}
if ($success) {
wp_update_attachment_metadata($post_id, $meta);
if ($target == 'thumbnail' || $target == 'all' || $target == 'full' && !array_key_exists('thumbnail', $meta['sizes'])) {
if ($thumb_url = get_attachment_icon_src($post_id)) {
$msg .= "thumbnail={$thumb_url[0]}";
}
}
} else {
$delete = true;
}
if ($delete) {
$delpath = apply_filters('wp_delete_file', $new_path);
//.........这里部分代码省略.........
示例8: resize
/**
* Scale down an image to fit a particular size and save a new copy of the image.
*
* The PNG transparency will be preserved using the function, as well as the
* image type. If the file going in is PNG, then the resized image is going to
* be PNG. The only supported image types are PNG, GIF, and JPEG.
*
* Some functionality requires API to exist, so some PHP version may lose out
* support. This is not the fault of WordPress (where functionality is
* downgraded, not actual defects), but of your PHP version.
*
* @since 2.5.0
*
* @param string $file Image file path.
* @param int $max_w Maximum width to resize to.
* @param int $max_h Maximum height to resize to.
* @param bool $crop Optional. Whether to crop image or resize.
* @param string $suffix Optional. File suffix.
* @param string $dest_path Optional. New image file path.
* @param int $jpeg_quality Optional, default is 95. Image quality percentage.
* @return mixed WP_Error on failure. String with new destination path.
*/
function resize( $max_w, $max_h, $crop = false, $zoom_if_need=true) {
if ( !is_resource( $this->orig_image ) ) {
$this -> error_msg ('Error: orig_image not loaded');
return FALSE;
}
//list($orig_w, $orig_h, $orig_type) = $this->size;
$orig_w=$this->orig_w;
$orig_h=$this->orig_h;
$orig_type=$this->orig_type;
if ( $orig_w == $max_w && $orig_h == $max_h ) return TRUE;
//echo $orig_w.' x '.$orig_h.'<br />';echo $max_w.' x '.$max_h.'<br />';
$dims = $this->image_resize_dimensions($orig_w, $orig_h, $max_w, $max_h, $crop, $zoom_if_need);
if ( !$dims ) {
$this -> error_msg ( 'error_getting_dimensions - Could not calculate resized image dimensions', false );
return FALSE;
}
list($dest_x, $dest_y, $src_x, $src_y, $dest_w, $dest_h, $src_w, $src_h) = $dims;
if (isset($GLOBALS['wp'])) $this->dest_image = wp_imagecreatetruecolor( $dest_w, $dest_h );
else $this->dest_image = imagecreatetruecolor( $dest_w, $dest_h );
imagecopyresampled( $this->dest_image, $this->orig_image, $dest_x, $dest_y, $src_x, $src_y, $dest_w, $dest_h, $src_w, $src_h);
$this->dest_w=$dest_w;
$this->dest_h=$dest_h;
// convert from full colors to index colors, like original PNG.
if ( IMAGETYPE_PNG == $orig_type && function_exists('imageistruecolor') && !imageistruecolor( $this->orig_image ) )
imagetruecolortopalette( $this->dest_image, false, imagecolorstotal( $this->orig_image ) );
// we don't need the original in memory anymore - depricated
//imagedestroy( $this->orig_image );
return TRUE;
}
示例9: ifp_wp_crop_image
/**
* Crop an Image to a given size.
* Modified version of the function wp_crop_image located in
* wp-admin/includes/image.php
*
* Mixes in some picture detection and treatment from image_resize in
* wp-includes/media.php
*
* @param string|int $src_file The source file or Attachment ID.
* @param int $src_x The start x position to crop from.
* @param int $src_y The start y position to crop from.
* @param int $src_w The width to crop.
* @param int $src_h The height to crop.
* @param int $dst_w The destination width.
* @param int $dst_h The destination height.
* @param int $src_abs Optional. If the source crop points are absolute.
* @param string $dst_file Optional. The destination file to write to.
* @return string|WP_Error|false New filepath on success, WP_Error or false on failure.
*/
function ifp_wp_crop_image($src_file, $src_x, $src_y, $src_w, $src_h, $dst_w, $dst_h, $src_abs = false, $dst_file = false)
{
if (is_numeric($src_file)) {
// Handle int as attachment ID
$src_file = get_attached_file($src_file);
}
$src = wp_load_image($src_file);
if (!is_resource($src)) {
return new WP_Error('error_loading_image', $src, $src_file);
}
/* Part from image_resize */
$size = @getimagesize($src_file);
if (!$size) {
return new WP_Error('invalid_image', __('Could not read image size'), $file);
}
list($orig_w, $orig_h, $orig_type) = $size;
/* End part from image_resize */
$dst = wp_imagecreatetruecolor($dst_w, $dst_h);
if ($src_abs) {
$src_w -= $src_x;
$src_h -= $src_y;
}
imagecopyresampled($dst, $src, 0, 0, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h);
/* Part from image_resize */
// convert from full colors to index colors, like original PNG.
if (IMAGETYPE_PNG == $orig_type && function_exists('imageistruecolor') && !imageistruecolor($src)) {
imagetruecolortopalette($dst, false, imagecolorstotal($src));
}
/* End part from image_resize */
imagedestroy($src);
// Free up memory
if (!$dst_file) {
$dst_file = str_replace(basename($src_file), 'cropped-' . basename($src_file), $src_file);
}
$info_src = pathinfo($src_file);
$info_dst = pathinfo($dst_file);
$dir = $info_dst['dirname'];
$ext = $info_src['extension'];
// Keep the source extension
$name = wp_basename($dst_file, ".{$info_dst['extension']}");
$dst_file = "{$dir}/{$name}.{$ext}";
if (IMAGETYPE_GIF == $orig_type) {
$success = imagegif($dst, $dst_file);
} else {
if (IMAGETYPE_PNG == $orig_type) {
$success = imagepng($dst, $dst_file);
} else {
$dst_file = "{$dir}/{$name}.jpg";
$success = imagejpeg($dst, $dst_file, apply_filters('jpeg_quality', 90, 'wp_crop_image'));
}
}
imagedestroy($dst);
if ($success) {
/* Part from image_resize */
// Set correct file permissions
$stat = stat(dirname($dst_file));
$perms = $stat['mode'] & 0666;
//same permissions as parent folder, strip off the executable bits
@chmod($dst_file, $perms);
/* End part from image_resize */
return $dst_file;
} else {
return false;
}
}
示例10: gllr_image_resize
function gllr_image_resize($file, $max_w, $max_h, $crop = false, $suffix = null, $dest_path = null, $jpeg_quality = 90)
{
$size = @getimagesize($file);
if (!$size) {
return new WP_Error('invalid_image', __('Image size not defined', 'gallery-plugin'), $file);
}
$type = $size[2];
if (3 == $type) {
$image = imagecreatefrompng($file);
} else {
if (2 == $type) {
$image = imagecreatefromjpeg($file);
} else {
if (1 == $type) {
$image = imagecreatefromgif($file);
} else {
if (15 == $type) {
$image = imagecreatefromwbmp($file);
} else {
if (16 == $type) {
$image = imagecreatefromxbm($file);
} else {
return new WP_Error('invalid_image', __('We can update only PNG, JPEG, GIF, WPMP or XBM filetype. For other, please, manually reload image.', 'gallery-plugin'), $file);
}
}
}
}
}
if (!is_resource($image)) {
return new WP_Error('error_loading_image', $image, $file);
}
/*$size = @getimagesize( $file );*/
list($orig_w, $orig_h, $orig_type) = $size;
$dims = gllr_image_resize_dimensions($orig_w, $orig_h, $max_w, $max_h, $crop);
if (!$dims) {
return new WP_Error('error_getting_dimensions', __('Image size changes not defined', 'gallery-plugin'));
}
list($dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h) = $dims;
$newimage = wp_imagecreatetruecolor($dst_w, $dst_h);
imagecopyresampled($newimage, $image, $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 && function_exists('imageistruecolor') && !imageistruecolor($image)) {
imagetruecolortopalette($newimage, false, imagecolorstotal($image));
}
/* We don't need the original in memory anymore */
imagedestroy($image);
/* $suffix will be appended to the destination filename, just before the extension */
if (!$suffix) {
$suffix = "{$dst_w}x{$dst_h}";
}
$info = pathinfo($file);
$dir = $info['dirname'];
$ext = $info['extension'];
$name = wp_basename($file, ".{$ext}");
if (!is_null($dest_path) and $_dest_path = realpath($dest_path)) {
$dir = $_dest_path;
}
$destfilename = "{$dir}/{$name}-{$suffix}.{$ext}";
if (IMAGETYPE_GIF == $orig_type) {
if (!imagegif($newimage, $destfilename)) {
return new WP_Error('resize_path_invalid', __('Invalid path', 'gallery-plugin'));
}
} elseif (IMAGETYPE_PNG == $orig_type) {
if (!imagepng($newimage, $destfilename)) {
return new WP_Error('resize_path_invalid', __('Invalid path', 'gallery-plugin'));
}
} else {
/* All other formats are converted to jpg */
$destfilename = "{$dir}/{$name}-{$suffix}.jpg";
if (!imagejpeg($newimage, $destfilename, apply_filters('jpeg_quality', $jpeg_quality, 'image_resize'))) {
return new WP_Error('resize_path_invalid', __('Invalid path', 'gallery-plugin'));
}
}
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;
}
示例11: _crop_image_resource
/**
* Crops an image resource. Internal use only.
*
* @since 2.9.0
*
* @ignore
* @param resource $img Image resource.
* @param float $x Source point x-coordinate.
* @param float $y Source point y-cooredinate.
* @param float $w Source width.
* @param float $h Source height.
* @return resource (maybe) cropped image resource.
*/
function _crop_image_resource($img, $x, $y, $w, $h)
{
$dst = wp_imagecreatetruecolor($w, $h);
if (is_resource($dst)) {
if (imagecopy($dst, $img, 0, 0, $x, $y, $w, $h)) {
imagedestroy($img);
$img = $dst;
}
}
return $img;
}
示例12: crop
/**
* LazyestImage::crop()
*
* @param mixed $size Width and Height of the square cropped image
* @return bool success or failure
* @todo merge with LazyestImage::resize()
*/
function crop($size)
{
if (false === $this->loc()) {
return false;
}
if (!$this->memory_ok()) {
return false;
}
$img_location = $this->original();
list($o_width, $o_height, $o_type) = @getimagesize($img_location);
$img = $this->load_image($img_location);
if (!is_resource($img)) {
trigger_error($img, E_USER_WARNING);
return false;
}
if ($o_width > $o_height) {
// landscape image
$out_width = $out_height = $o_height;
$out_left = round(($o_width - $o_height) / 2);
$out_top = 0;
} else {
// portrait image
$out_top = 0;
$out_width = $out_height = $o_width;
$out_left = 0;
}
$cropped = wp_imagecreatetruecolor($size, $size);
imagecopyresampled($cropped, $img, 0, 0, $out_left, $out_top, $size, $size, $out_width, $out_height);
// convert from full colors to index colors, like original PNG.
if (IMAGETYPE_PNG == $o_type && function_exists('imageistruecolor') && !imageistruecolor($img)) {
imagetruecolortopalette($cropped, false, imagecolorstotal($img));
}
unset($img);
$this->resized = $cropped;
$this->reset_orientation();
$this->resized = apply_filters('lazyest_image_cropped', $this->resized, $size, $this);
return true;
}
示例13: flip
/**
* Flips current image.
*
* @since 3.5.0
* @access public
*
* @param boolean $horz Flip along Horizontal Axis
* @param boolean $vert Flip along Vertical Axis
* @returns boolean|WP_Error
*/
public function flip($horz, $vert)
{
$w = $this->size['width'];
$h = $this->size['height'];
$dst = wp_imagecreatetruecolor($w, $h);
if (is_resource($dst)) {
$sx = $vert ? $w - 1 : 0;
$sy = $horz ? $h - 1 : 0;
$sw = $vert ? -$w : $w;
$sh = $horz ? -$h : $h;
if (imagecopyresampled($dst, $this->image, 0, 0, $sx, $sy, $w, $h, $sw, $sh)) {
imagedestroy($this->image);
$this->image = $dst;
return true;
}
}
return new WP_Error('image_flip_error', __('Image flip failed.'), $this->file);
}
示例14: wp_save_image
function wp_save_image($post_id)
{
$return = new stdClass();
$success = $delete = $scaled = $nocrop = false;
$post = get_post($post_id);
@ini_set('memory_limit', apply_filters('admin_memory_limit', WP_MAX_MEMORY_LIMIT));
$img = load_image_to_edit($post_id, $post->post_mime_type);
if (!is_resource($img)) {
$return->error = esc_js(__('Unable to create new image.'));
return $return;
}
$fwidth = !empty($_REQUEST['fwidth']) ? intval($_REQUEST['fwidth']) : 0;
$fheight = !empty($_REQUEST['fheight']) ? intval($_REQUEST['fheight']) : 0;
$target = !empty($_REQUEST['target']) ? preg_replace('/[^a-z0-9_-]+/i', '', $_REQUEST['target']) : '';
$scale = !empty($_REQUEST['do']) && 'scale' == $_REQUEST['do'];
if ($scale && $fwidth > 0 && $fheight > 0) {
$sX = imagesx($img);
$sY = imagesy($img);
// check if it has roughly the same w / h ratio
$diff = round($sX / $sY, 2) - round($fwidth / $fheight, 2);
if (-0.1 < $diff && $diff < 0.1) {
// scale the full size image
$dst = wp_imagecreatetruecolor($fwidth, $fheight);
if (imagecopyresampled($dst, $img, 0, 0, 0, 0, $fwidth, $fheight, $sX, $sY)) {
imagedestroy($img);
$img = $dst;
$scaled = true;
}
}
if (!$scaled) {
$return->error = esc_js(__('Error while saving the scaled image. Please reload the page and try again.'));
return $return;
}
} elseif (!empty($_REQUEST['history'])) {
$changes = json_decode(stripslashes($_REQUEST['history']));
if ($changes) {
$img = image_edit_apply_changes($img, $changes);
}
} else {
$return->error = esc_js(__('Nothing to save, the image has not changed.'));
return $return;
}
$meta = wp_get_attachment_metadata($post_id);
$backup_sizes = get_post_meta($post->ID, '_wp_attachment_backup_sizes', true);
if (!is_array($meta)) {
$return->error = esc_js(__('Image data does not exist. Please re-upload the image.'));
return $return;
}
if (!is_array($backup_sizes)) {
$backup_sizes = array();
}
// generate new filename
$path = get_attached_file($post_id);
$path_parts = pathinfo($path);
$filename = $path_parts['filename'];
$suffix = time() . rand(100, 999);
if (defined('IMAGE_EDIT_OVERWRITE') && IMAGE_EDIT_OVERWRITE && isset($backup_sizes['full-orig']) && $backup_sizes['full-orig']['file'] != $path_parts['basename']) {
if ('thumbnail' == $target) {
$new_path = "{$path_parts['dirname']}/{$filename}-temp.{$path_parts['extension']}";
} else {
$new_path = $path;
}
} else {
while (true) {
$filename = preg_replace('/-e([0-9]+)$/', '', $filename);
$filename .= "-e{$suffix}";
$new_filename = "{$filename}.{$path_parts['extension']}";
$new_path = "{$path_parts['dirname']}/{$new_filename}";
if (file_exists($new_path)) {
$suffix++;
} else {
break;
}
}
}
// save the full-size file, also needed to create sub-sizes
if (!wp_save_image_file($new_path, $img, $post->post_mime_type, $post_id)) {
$return->error = esc_js(__('Unable to save the image.'));
return $return;
}
if ('nothumb' == $target || 'all' == $target || 'full' == $target || $scaled) {
$tag = false;
if (isset($backup_sizes['full-orig'])) {
if ((!defined('IMAGE_EDIT_OVERWRITE') || !IMAGE_EDIT_OVERWRITE) && $backup_sizes['full-orig']['file'] != $path_parts['basename']) {
$tag = "full-{$suffix}";
}
} else {
$tag = 'full-orig';
}
if ($tag) {
$backup_sizes[$tag] = array('width' => $meta['width'], 'height' => $meta['height'], 'file' => $path_parts['basename']);
}
$success = update_attached_file($post_id, $new_path);
$meta['file'] = _wp_relative_upload_path($new_path);
$meta['width'] = imagesx($img);
$meta['height'] = imagesy($img);
list($uwidth, $uheight) = wp_constrain_dimensions($meta['width'], $meta['height'], 128, 96);
$meta['hwstring_small'] = "height='{$uheight}' width='{$uwidth}'";
if ($success && ('nothumb' == $target || 'all' == $target)) {
$sizes = get_intermediate_image_sizes();
//.........这里部分代码省略.........
示例15: ulogin_get_user_photo
/**
* Получение аватара пользователя
* @param $u_user
* @param $user_id
* @return string
*/
function ulogin_get_user_photo($u_user, $user_id)
{
$q = true;
$validate_gravatar = ulogin_validate_gravatar('', $user_id);
if ($validate_gravatar) {
update_user_meta($user_id, 'ulogin_photo_gravatar', 1);
return false;
}
delete_user_meta($user_id, 'ulogin_photo_gravatar');
$u_user['photo'] = $u_user['photo'] === "https://ulogin.ru/img/photo.png" ? '' : $u_user['photo'];
$u_user['photo_big'] = $u_user['photo_big'] === "https://ulogin.ru/img/photo_big.png" ? '' : $u_user['photo_big'];
$file_url = (isset($u_user['photo_big']) and !empty($u_user['photo_big'])) ? $u_user['photo_big'] : ((isset($u_user['photo']) and !empty($u_user['photo'])) ? $u_user['photo'] : '');
if (empty($file_url)) {
return false;
}
//directory to import to
$avatar_dir = str_replace('\\', '/', dirname(dirname(dirname(dirname(__FILE__))))) . '/wp-content/uploads/';
if (!file_exists($avatar_dir)) {
$q = mkdir($avatar_dir);
}
$avatar_dir .= 'ulogin_avatars/';
if ($q && !file_exists($avatar_dir)) {
$q = mkdir($avatar_dir);
}
if (!$q) {
return false;
}
$response = ulogin_get_response($file_url, false);
$response = !$response && in_array('curl', get_loaded_extensions()) ? file_get_contents($file_url) : $response;
if (!$response) {
return false;
}
$filename = $u_user['network'] . '_' . $u_user['uid'];
$file_addr = $avatar_dir . $filename;
$handle = fopen($file_addr, "w");
$fileSize = fwrite($handle, $response);
fclose($handle);
if (!$fileSize) {
@unlink($file_addr);
return false;
}
list($width, $height, $type) = getimagesize($file_addr);
if ($width / $height > 1) {
$max_size = $width;
} else {
$max_size = $height;
}
$thumb = wp_imagecreatetruecolor($max_size, $max_size);
if (!is_resource($thumb)) {
@unlink($file_addr);
return false;
}
switch ($type) {
case IMAGETYPE_GIF:
$res = '.gif';
$source = imagecreatefromgif($file_addr);
break;
case IMAGETYPE_JPEG:
$res = '.jpg';
$source = imagecreatefromjpeg($file_addr);
break;
case IMAGETYPE_PNG:
$res = '.png';
$source = imagecreatefrompng($file_addr);
break;
default:
$res = '.jpg';
$source = imagecreatefromjpeg($file_addr);
break;
}
if (imagecopy($thumb, $source, ($max_size - $width) / 2, ($max_size - $height) / 2, 0, 0, $width, $height)) {
imagedestroy($source);
@unlink($file_addr);
} else {
@unlink($file_addr);
return false;
}
$filename = $filename . $res;
switch ($type) {
case IMAGETYPE_GIF:
imagegif($thumb, $avatar_dir . $filename);
break;
case IMAGETYPE_JPEG:
imagejpeg($thumb, $avatar_dir . $filename);
break;
case IMAGETYPE_PNG:
imagepng($thumb, $avatar_dir . $filename);
break;
default:
imagejpeg($thumb, $avatar_dir . $filename);
break;
}
imagedestroy($thumb);
return home_url() . '/wp-content/uploads/ulogin_avatars/' . $filename;
//.........这里部分代码省略.........