Dynamically generate a thumbnail (if one is not already available) and return the url.

@param  integer $attachment_id
@param  integer $width
@param  integer $height
@param  boolean $crop
@return string
490497
public function thumbnail( $attachment_id, $width, $height, $crop = true ) {
		$width = absint( $width );
		$height = absint( $height );

		$upload_dir = wp_upload_dir();
		$attachment = wp_get_attachment_metadata( $attachment_id );
		$source = MixedType::normalizePath( get_attached_file( $attachment_id ) );

		if ( ! $attachment || ! file_exists( $source ) ) {
			return '';
		}

		$attachment_subdirectory = preg_replace( '/\/?[^\/]+\z/', '', $attachment['file'] );
		$filename = $this->getResizedFilename( $source, $width, $height, $crop );
		$destination = MixedType::normalizePath( MixedType::normalizePath( $upload_dir['basedir'] ) . DIRECTORY_SEPARATOR . $attachment_subdirectory ) . DIRECTORY_SEPARATOR . $filename;

		$stored = $this->store( $source, $destination, $width, $height, $crop );

		if ( empty( $stored ) ) {
			return '';
		}

		$fileurl = trailingslashit( $upload_dir['baseurl'] ) . $attachment_subdirectory . '/' . $filename;

		return $fileurl;
	}
474055
private function update_attachment_metadata_for_image_size( $id, $new_metadata, $image_size ) {
		$metadata = wp_get_attachment_metadata( $id );

		if ( ! is_array( $metadata ) ) {
			return false;
		}

		// If have metadata for image_size.
		if ( ! empty( $new_metadata['sizes'][ $image_size ] ) ) {
			$metadata['sizes'][ $image_size ] = $new_metadata['sizes'][ $image_size ];
			wp_update_attachment_metadata( $id, $metadata );
			return true;
		}

		// Else remove unused metadata if any.
		if ( ! empty( $metadata['sizes'][ $image_size ] ) ) {
			unset( $metadata['sizes'][ $image_size ] );
			wp_update_attachment_metadata( $id, $metadata );
			// Treat removing unused metadata as no change.
		}
		return false;
	}
809368
final public function crop($x = 0, $y = 0, $width = null, $height = null)
	{
		$this->_image_handle = imagecrop(
			$this->_image_handle,
			array_merge(
				array(
					'x' => 0,
					'y' => 0,
					'width' => $this->imageWidth(),
					'height' => $this->imageHeight()
				),
				array_filter(
					array_combine(
						array('x', 'y', 'width', 'height'),
						array($x, $y, $width, $height)
					)
				)
			)
		);

		return $this;
	}
82107
public function crop($width, $height, $mode = 'tl')
    {
        if ( !is_numeric($width) ) {
            throw new \InvalidArgumentException('$width must be numeric');
        }

        if ( !is_numeric($height) ) {
            throw new \InvalidArgumentException('$height must be numeric');
        }

        if ( $this->newImage )
        {
            $this->sourceImage = $this->newImage;
        }

        $oldWidth   = ($this->newImage) ? imagesx($this->newImage) : $this->source['width'];
        $oldHeight  = ($this->newImage) ? imagesy($this->newImage) : $this->source['height'];

        $this->create($width, $height);

        $startX     = $startY = 0;
        $cropWidth  = $sourceWidth  = $width;
        $cropHeight = $sourceHeight = $height;

        if ( is_array($mode) )
        {
            $startX = $mode[0];
            $startY = $mode[1];
        }

        if ($mode === self::CROP_TOP_CENTER)
        {
            $startX = ($oldWidth - $cropWidth) / 2;
        }
        else if ($mode === self::CROP_TOP_RIGHT)
        {
            $startX = $oldWidth - $cropWidth;
        }
        else if ($mode === self::CROP_CENTER_LEFT)
        {
            $startY = ($oldHeight - $cropHeight) / 2;
        }
        else if ($mode === self::CROP_CENTER_CENTER)
        {
            $startX = ($oldWidth - $cropWidth)   / 2;
            $startY = ($oldHeight - $cropHeight) / 2;
        }
        else if ($mode === self::CROP_CENTER_RIGHT)
        {
            $startX = $oldWidth - $cropWidth;
            $startY = ($oldHeight - $cropHeight) / 2;
        }
        else if ($mode === self::CROP_BOTTOM_LEFT)
        {
            $startY = $oldHeight - $cropHeight;
        }
        else if ($mode === self::CROP_BOTTOM_CENTER)
        {
            $startX = ($oldWidth - $cropWidth)   / 2;
            $startY = $oldHeight - $cropHeight;
        }
        else if ($mode === self::CROP_BOTTOM_RIGHT)
        {
            $startX = $oldWidth - $cropWidth;
            $startY = $oldHeight - $cropHeight;
        }
        else
        {
        }

        imagecopyresampled(
            $this->newImage,
            $this->sourceImage,
            0, 0, $startX, $startY,
            $cropWidth, $cropHeight, $sourceWidth, $sourceHeight);

        return $this;
    }
1082670
public function getURL($width = '', $height = '')
    {
        $uri = $this->getName();
        if ($height && $width) {
            return Settings::get('image_url').$width.'/'.$height.'/'.$uri;
        } elseif ($width) {
            return Settings::get('image_url').$width.'/'.$uri;
        } else { // original image
            return Settings::get('image_url').$uri;
        }
    }
65666
public function sizeUrl($size)
    {
        if (($this->base['return_format'] === 'array') && isset($this->base['value']['sizes'][$size])) {
            return $this->base['value']['sizes'][$size];
        } elseif ($this->base['return_format'] === 'id') {
            if ($src = wp_get_attachment_image_src($this->id(), $size)) {
                return $src[0];
            }
        }

        return '';
    }
599719
public function getPostAttachment($size = 'full', $property = null)
    {
        $post_id = $this->get('ID');
        if (!has_post_thumbnail($post_id)) {
            return false;
        }
        
        $attachment_id = get_post_thumbnail_id($post_id);
        $image_properties = array(
            'url',
            'width',
            'height',
            'is_resized'
        );
        $image_array = array_combine(
            $image_properties,
            array_values(wp_get_attachment_image_src($attachment_id, $size))
        );
        if (in_array($property, $image_properties)) {
            return $image_array[$property];
        }
        return $image_array;
    }
356644
public function actionShow($id, $width = null, $height = null)
    {
        $model = $this->findModel($id);
        $response = Yii::$app->getResponse();
        if ($width === null && $height === null) {
            return $response->sendFile($model->filename, $model->name, [
                    'mimeType' => $model->type,
                    'fileSize' => $model->size,
                    'inline' => true
            ]);
        } else {
            $dir = '';
            if ($width !== null) {
                $dir .= 'w' . (int) $width;
            }
            if ($height !== null) {
                $dir .= 'h' . (int) $height;
            }
            $filename = sprintf('%s/%s/%x/%d_%s', Yii::getAlias($this->savePath), $dir, $id % 255, $id, $model->name);
            if (!file_exists($filename)) {
                FileHelper::createDirectory(dirname($filename));
                $image = Image::thumbnail($model->filename, $width, $height);
                $image->save($filename);
            }
            return $response->sendFile($filename, $model->name, [
                    'mimeType' => $model->type,
                    'inline' => true
            ]);
        }
    }
489996
private function timber_generate_sizes( $attachment_id ) {
		$sizes      = [];
		$img_sizes  = Helper::get_image_sizes();
		$attachment = get_post( $attachment_id );

		/**
		 * Never automatically generate image sizes on upload for SVG and GIF images.
		 * SVG and GIF images will still be resized when requested on the fly.
		 */
		if ( in_array( $attachment->post_mime_type, array(
			'image/svg+xml',
			'image/gif',
		), true ) ) {
			return $sizes;
		}

		// Timber needs the file src as an URL
		$file_src  = wp_get_attachment_url( $attachment_id );
		$meta_data = wp_get_attachment_metadata( $attachment_id );

		/**
		 * Delete all existing image sizes for that file.
		 *
		 * This way, when Regenerate Thumbnails will be used, all non-registered image sizes will be
		 * deleted as well. Because Timber creates image sizes when they’re needed, we can safely do
		 * this.
		 */
		Timber\ImageHelper::delete_generated_files( $file_src );

		foreach ( $img_sizes as $key => $img_size ) {
			if ( ! $this->timber_should_resize( $attachment->post_parent, $img_size ) ) {
				continue;
			}

			// Create downsized version of the image
			$downsized = image_downsize( $attachment_id, $key );

			// Bail out if there was an error while downsizing the image.
			if ( ! is_array( $downsized ) ) {
				continue;
			}

			list( $file_src, $file_width, $file_height ) = $downsized;

			/**
			 * Create an image definition array that will be used for attachment metadata.
			 *
			 * WordPress saves an array with the file src, the width, the height and the mime type
			 * of an attachment in the postmeta database table. By generating this array and saving
			 * it in the database, we can improve the compatibility with the core image
			 * functionality.
			 *
			 * Correct width and height values of '0' by calculating them from the original image
			 * ratio, if meta data is available.
			 */
			if ( ! empty( $meta_data['height'] ) && ! empty( $meta_data['width'] ) ) {
				if ( 0 === (int) $file_width && ! empty( $meta_data['height'] ) ) {
					$file_width = round( $file_height / $meta_data['height'] * $meta_data['width'] );
				} elseif ( 0 === (int) $file_height ) {
					$file_height = round( $file_width / $meta_data['width'] * $meta_data['height'] );
				}
			}

			if ( is_numeric( $file_width ) && is_numeric( $file_height ) ) {
				$sizes[ $key ] = [
					'file'      => wp_basename( $file_src ),
					'width'     => $file_width,
					'height'    => $file_height,
					'mime-type' => wp_check_filetype( $file_src )['type'],
				];
			}

			/**
			 * Filters whether srcset sizes should be generated when an image is uploaded.
			 *
			 * @since 0.13.0
			 *
			 * @param bool     $generate_srcset_sizes Whether to generate srcset sizes. Passing false will prevent
			 *                                        srcset sizes to generated when an image is uploaded. Default false.
			 * @param string   $key                   The image size key.
			 * @param array    $img_size              The image size configuration array.
			 * @param \WP_Post $attachment            The attachment post.
			 */
			$generate_srcset_sizes = apply_filters( 'timmy/generate_srcset_sizes', false, $key, $img_size, $attachment );

			// Get setting from image configuration.
			$generate_srcset_sizes = isset( $img_size['generate_srcset_sizes'] )
				? $img_size['generate_srcset_sizes']
				: $generate_srcset_sizes;

			if ( false === $generate_srcset_sizes ) {
				continue;
			}

			// Get values for the default image
			$crop  = Helper::get_crop_for_size( $img_size );
			$force = Helper::get_force_for_size( $img_size );

			// Generate additional image sizes used for srcset
			if ( isset( $img_size['srcset'] ) ) {
				foreach ( $img_size['srcset'] as $srcset_size ) {
					list( $width, $height ) = Helper::get_dimensions_for_srcset_size( $img_size['resize'], $srcset_size );

					// For the new source, we use the same $crop and $force values as the default image
					self::resize( $img_size, $file_src, $width, $height, $crop, $force );
				}
			}
		}

		return $sizes;
	}
32819
public function getPicUrl($file_id, $width = 0, $height = 0,$water=false, $crop = self::resize_rect_in,$blur=-1){
        $path=$this->config['base_path'].DIRECTORY_SEPARATOR.$file_id;
        return $this->config['cdn'].$path;
    }
837012
public static function getAttachmentIdFromUrl($attachmentUrl = '')
    {
        if (empty($attachmentUrl)) {
            return;
        }
        global $wpdb;
        $attachmentId = false;
        // Get the upload directory paths
        $upload_dir_paths = wp_upload_dir();
        // Make sure the upload path base directory exists in the attachment URL,
        // to verify that we're working with a media library image
        if (false !== strpos($attachmentUrl, $upload_dir_paths['baseurl'])) {
            // If this is the URL of an auto-generated thumbnail, get the URL of the original image
            $attachmentUrl = preg_replace('/-\d+x\d+(?=\.(jpg|jpeg|png|gif)$)/i', '', $attachmentUrl);
            // Remove the upload path base directory from the attachment URL
            $attachmentUrl = str_replace($upload_dir_paths['baseurl'] . '/', '', $attachmentUrl);
            // Finally, run a custom database query to get the attachment ID from the modified
            // attachment URL
            $attachmentId = $wpdb->get_var(
                $wpdb->prepare(
                    "SELECT wposts.ID
					FROM {$wpdb->posts} wposts, {$wpdb->postmeta} wpostmeta
					WHERE wposts.ID = wpostmeta.post_id
					AND wpostmeta.meta_key = '_wp_attached_file'
					AND wpostmeta.meta_value = '%s'
					AND wposts.post_type = 'attachment'", $attachmentUrl));
        }
        return $attachmentId;
    }
791220
private function crop($optimal_width, $optimal_height, $new_width, $new_height)
    {
        // Find center - this will be used for the crop
        $crop_start_x = ($optimal_width  / 2) - ($new_width  / 2);
        $crop_start_y = ($optimal_height / 2) - ($new_height / 2);

        $crop = $this->image_resized;

        $dest_offset_x = max(0, -$crop_start_x);
        $dest_offset_y = max(0, -$crop_start_y);
        $crop_start_x  = max(0, $crop_start_x);
        $crop_start_y  = max(0, $crop_start_y);
        $dest_width    = min($optimal_width, $new_width);
        $dest_height   = min($optimal_height, $new_height);

        // Now crop from center to exact requested size
        $this->image_resized = imagecreatetruecolor($new_width, $new_height);

        imagealphablending($crop, true);
        imagealphablending($this->image_resized, false);
        imagesavealpha($this->image_resized, true);

        imagefilledrectangle($this->image_resized, 0, 0, $new_width, $new_height,
            imagecolorallocatealpha($this->image_resized, 255, 255, 255, 127)
        );

        imagecopyresampled($this->image_resized, $crop, $dest_offset_x, $dest_offset_y, $crop_start_x, $crop_start_y, $dest_width, $dest_height, $dest_width, $dest_height);

        return true;
    }
445691
public function _async_attachment_images( $atts, $attachment, $size ) {
		if ( ! $this->_is_async_attachment_images() ) {
			return $atts;
		}

		$atts['data-src'] = $atts['src'];
		$atts['src']      = wp_get_attachment_image_url( $attachment->ID, 'wppso-minimum-thumbnail' );

		if ( isset( $atts['srcset'] ) ) {
			$atts['data-srcset'] = $atts['srcset'];
			$atts['srcset']      = null;
		}

		$atts['decoding'] = 'async';
		return $atts;
	}
1301721
protected function getAvatarUrl( $id, $size ) {
		$attachments_fallback_chain = $this->getAttachmentFallbackChain( $id );

		foreach ( $attachments_fallback_chain as $attachment_id ) {
			$image = wp_get_attachment_image_src( $attachment_id, $size );
			if ( ! empty( $image ) ) {
				return $image[0];
			}
		}

		return null;
	}
1006726
public static function url($relative, $width, $height)
    {

        if (preg_match('/^https?:\/\//i', $relative)) {

            return static::fromUrl($relative, $width, $height);

        } elseif (strlen(parse_url($relative, PHP_URL_QUERY)) > 0) {

            $relative = static::$config['base_uri'] . $relative;

            return static::fromUrl($relative, $width, $height);
        }

        return static::fromFile($relative, $width, $height);
    }
1349386
public function crop(string $cropMethod, int $width, int $height)
    {
        if (! $this->validateManipulation($cropMethod, 'crop')) {
            throw InvalidManipulation::invalidParameter(
                'cropmethod',
                $cropMethod,
                $this->getValidManipulationOptions('crop')
            );
        }

        $this->width($width);
        $this->height($height);

        return $this->addManipulation('crop', $cropMethod);
    }
91312
public function get_thumb_url($image_url, $orig_width, $orig_height, $thumb_width = 75, $force = false) {
        global $OUTPUT;

        if (!$force && $orig_width <= $thumb_width && $orig_height <= $thumb_width) {
            return $image_url;
        } else {
            $thumb_url = '';
            $commons_main_dir = 'https://upload.wikimedia.org/wikipedia/commons/';
            if ($image_url) {
                $short_path = str_replace($commons_main_dir, '', $image_url);
                $extension = strtolower(pathinfo($short_path, PATHINFO_EXTENSION));
                if (strcmp($extension, 'gif') == 0) {  //no thumb for gifs
                    return $OUTPUT->image_url(file_extension_icon('.gif', $thumb_width))->out(false);
                }
                $dir_parts = explode('/', $short_path);
                $file_name = end($dir_parts);
                if ($orig_height > $orig_width) {
                    $thumb_width = round($thumb_width * $orig_width/$orig_height);
                }
                $thumb_url = $commons_main_dir . 'thumb/' . implode('/', $dir_parts) . '/'. $thumb_width .'px-' . $file_name;
                if (strcmp($extension, 'svg') == 0) {  //png thumb for svg-s
                    $thumb_url .= '.png';
                }
            }
            return $thumb_url;
        }
    }