Extract url from zip and load into WP using media_handle_sideload()
Will return an empty string if something went wrong.

@param string $img_id

@return string
423305
protected function fetchAndSaveUniqueImage( $img_id ) {

		// Cheap cache
		static $already_done = [];
		if ( isset( $already_done[ $img_id ] ) ) {
			return $already_done[ $img_id ];
		}

		/* Process */
		// Get target path
		$img_location = $this->getTargetPath( self::IMAGE_SCHEMA, $img_id );

		// Basename without query string
		$filename = explode( '?', basename( $img_location ) );
		$filename = array_shift( $filename );

		$filename = sanitize_file_name( urldecode( $filename ) );

		if ( ! preg_match( '/\.(jpe?g|gif|png)$/i', $filename ) ) {
			// Unsupported image type
			$already_done[ $img_id ] = '';

			return '';
		}

		$image_content = $this->getZipContent( $img_location, false );

		if ( ! $image_content ) {
			// try a different directory in the zip container
			try {
				$alt_img_location = 'word/' . $img_location;
				$image_content = $this->getZipContent( $alt_img_location, false );

				if ( ! $image_content ) {
					throw new \Exception( 'Image could not be retrieved in the DOCX file with Pressbooks\Import\Ooxml\fetchAndSaveUniqueImage()' );
				}
			} catch ( \Exception $exc ) {
				$this->log( $exc->getMessage() );
				$already_done[ $img_location ] = '';

				return '';
			}
		}

		$tmp_name = $this->createTmpFile();
		\Pressbooks\Utility\put_contents( $tmp_name, $image_content );

		if ( ! \Pressbooks\Image\is_valid_image( $tmp_name, $filename ) ) {

			try { // changing the file name so that extension matches the mime type
				$filename = $this->properImageExtension( $tmp_name, $filename );

				if ( ! \Pressbooks\Image\is_valid_image( $tmp_name, $filename ) ) {
					throw new \Exception( 'Image is corrupt, and file extension matches the mime type' );
				}
			} catch ( \Exception $exc ) {
				// Garbage, Don't import
				$already_done[ $img_location ] = '';

				return '';
			}
		}

		$pid = media_handle_sideload(
			[
				'name' => $filename,
				'tmp_name' => $tmp_name,
			], 0
		);
		$src = wp_get_attachment_url( $pid );
		if ( ! $src ) {
			$src = ''; // Change false to empty string
		}
		$already_done[ $img_location ] = $src;

		return $src;
	}
446437
public function getMedia($media_id, $is_video = false)
    {
        if (!$this->access_token && !$this->getAccessToken()) {
            return false;
        }
        $result = Tools::httpGet(self::API_URL_PREFIX . self::MEDIA_GET_URL . "access_token={$this->access_token}" . '&media_id=' . $media_id);
        if ($result) {
            if (is_string($result)) {
                $json = json_decode($result, true);
                if (isset($json['errcode'])) {
                    $this->errCode = $json['errcode'];
                    $this->errMsg = $json['errmsg'];
                    return $this->checkRetry(__FUNCTION__, func_get_args());
                }
            }
            return $result;
        }
        return false;
    }
206393
def get_photo_url(photo_id):
    
    args = _get_request_args(
        'flickr.photos.getSizes',
        photo_id=photo_id
    )
    resp = requests.post(API_URL, data=args)
    resp_json = json.loads(resp.text.encode('utf-8'))
    logger.debug(json.dumps(resp_json, indent=2))
    size_list = resp_json['sizes']['size']
    size_list_len = len(size_list)
    global image_size_mode
    image_size_mode = size_list_len if size_list_len < image_size_mode \
        else image_size_mode
    download_url = resp_json['sizes']['size'][-image_size_mode]['source']
    return download_url
1139866
public function formatUrl($slug, $blogId)
    {
        $this->load->model('blog/blog_model');
        return $this->blog_model->getBlogUrl($blogId) . '/' . $slug;
    }
139095
static function thumbnailFromUserId( $user_id, $size ) {

		$image_url = get_user_meta( $user_id, 'pb_catalog_logo', true );
		$book = get_active_blog_for_user( $user_id );
		if ( $book ) {
			switch_to_blog( $book->blog_id );
			$image_url = \Pressbooks\Image\thumbnail_from_url( $image_url, $size );
			restore_current_blog();
		}

		return $image_url;
	}
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
            ]);
        }
    }
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;
	}
546936
public static function sideload_image( $file ) {
		$loc = self::get_sideloaded_file_loc($file);
		if ( file_exists($loc) ) {
			return URLHelper::file_system_to_url($loc);
		}
		// Download file to temp location
		if ( !function_exists('download_url') ) {
			require_once ABSPATH.'/wp-admin/includes/file.php';
		}
		$tmp = download_url($file);
		preg_match('/[^\?]+\.(jpe?g|jpe|gif|png)\b/i', $file, $matches);
		$file_array = array();
		$file_array['name'] = basename($matches[0]);
		$file_array['tmp_name'] = $tmp;
		// If error storing temporarily, unlink
		if ( is_wp_error($tmp) ) {
			@unlink($file_array['tmp_name']);
			$file_array['tmp_name'] = '';
		}
		// do the validation and storage stuff
		$locinfo = pathinfo($loc);
		$file = wp_upload_bits($locinfo['basename'], null, file_get_contents($file_array['tmp_name']));
		return $file['url'];
	}
817969
public function get_url( $size ) {
		$url = null;
		if ( isset( $this->struct['return_type'] ) && 'url' === $this->struct['return_type'] ) {
			$url = $this->get_value();
		} else {
			$data = wp_get_attachment_image_src( $this->get_value(), $size );
			if ( ! empty( $data ) ) {
				$url = $data[0];
			}
		}

		return $url;
	}
1087891
public function getMedia($media_id) {
		$data = $this->getData('/cgi-bin/media/get', [
			'access_token' => $this->getAccessToken(),
			'media_id' => $media_id,
		]);

		return $this->errcode == 0 ? $this->saveFile($data) : null;
	}
336053
protected function _add_data_src_to_content_image( $image, $image_id ) {
		return preg_replace_callback(
			'@(<img decoding="async"[^>]*?) src="([^"]+?)"([^>]*?>)@m',
			function( $matches ) use ( $image_id ) {
				return sprintf(
					'%s src="%s" data-src="%s" %s',
					$matches[1],
					wp_get_attachment_image_url( $image_id, 'wppso-minimum-thumbnail' ),
					$matches[2],
					$matches[3]
				);
			},
			$image
		);
	}
137607
public function getCoverUrl($id, $type)
    {
        switch ($type) {
            case self::ITEM_TYPE_ANIMATION:
                return $this->browser->getHost().'/'.$type.'/img/'.(ceil($id / 1000) * 1000).'/'.$id.'/1.jpg';
            case self::ITEM_TYPE_CINEMA:
                return $this->browser->getHost().'/'.$type.'/img/'.(ceil($id / 10000) * 10000).'/'.$id.'/1.jpg';
            default:
                return;
        }
    }
1098218
public function getSourceUrlById($id) {
        $image = instance(new Model_Image)->fetchById($id);
        if (!$image) {
            throw new InvalidArgumentException(self::ERROR_IMAGE_NOT_FOUND);
        }
        return $this->getSourceUrl($image['filename']);
    }
323823
public static function get_image_id( $url ) {
		global $wpdb;
		if ( empty( $url ) ) {
			return 0;
		}

		$attachment = wp_cache_get( 'kirki_image_id_' . md5( $url ), null );
		if ( false === $attachment ) {
			$attachment = $wpdb->get_col( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE guid = %s;", $url ) ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery
			wp_cache_add( 'kirki_image_id_' . md5( $url ), $attachment, null );
		}

		if ( ! empty( $attachment ) ) {
			return $attachment[0];
		}
		return 0;
	}
338360
private function getImage($id)
    {
        $image = $this->getImageRepository()->find($id);

        if (empty($image)) {
            throw $this->createNotFoundException(sprintf('Unable to find image by ID "%d".', $id));
        }

        return $image;
    }
1217429
function GetThumb($dir, $md5, $filename, $mode, $width = 100, $height = 100)
    {
        $path = realpath(DIR_ROOT . '/' . $dir);
        if (is_file($path . '/.thumbs/' . $md5 . '_' . $width . '_' . $height . '_' . $mode . '.jpg'))
        {
            return $dir . '/.thumbs/' . $md5 . '_' . $width . '_' . $height . '_' . $mode . '.jpg';
        }

        $t = new Image_Toolbox($path . '/' . $filename);
        $t->newOutputSize($width, $height, $mode, false, '#FFFFFF');
        $filename = '/.thumbs/' . $md5 . '_' . $width . '_' . $height . '_' . $mode . '.jpg';
        $t->save($path . $filename, 'jpg', 80);
        chmod($path . $filename, 0666);
        return $dir . $filename;
    }
851672
public static function images($id)
    {
        $sizes = self::imageSizes();
        $urls = [];

        if(has_post_thumbnail($id))
        {
            $image = wp_get_attachment_image_src(
                get_post_thumbnail_id($id),
                'full'
            );

            $image = pathinfo($image[0]);
            $extension = $image['extension'];
            $image = $image['dirname'] . '/' . $image['filename'];
            $urls['full'] = $image . '.' . $extension;

            foreach($sizes as $size)
            {
                $urls[Data::spaceToCamelCase($size->name, '-')] = $image . '-' . $size->width . 'x' . $size->height . '.' . $extension;
            }

            return (object) $urls;
        }
        else
        {
            return false;
        }
    }