Re: How to get a Image from a URL?
Posted: 2009-02-10T18:18:25-07:00
The http protocol is not accepted by MagickWand for PHP for security reasons.
Use https://github.com/ImageMagick/ImageMagick/discussions instead.
https://imagemagick.com/discourse-server/
https://imagemagick.com/discourse-server/viewtopic.php?t=13111
Code: Select all
if (PG(allow_url_fopen)) {
/* Allowed to open remotely */
} else {
/* Not allowed */
}
Code: Select all
<?php
$wand = NewMagickWand();
// this fopen checks allow_url_fopen setting
$fp = fopen('http://example.com/image.png', 'r');
MagickReadImageFile($wand, $fp);
Code: Select all
<?php
$fp = fopen("http://server/image.png", "r");
$wand = NewMagickWand();
MagickReadImageFile($wand, $fp);
var_dump(MagickGetNumberImages($wand));
Code: Select all
Index: magickwand.c
===================================================================
--- magickwand.c (revision 14082)
+++ magickwand.c (working copy)
@@ -97,6 +97,9 @@
If one of the pseudo-formats is found, an attempt to read it into the MagickWand is made. If the "VID:" or
"TILE:" pseudo-formats are found, the rest of the format string is supposed to be a filename, and it is checked
for PHP accessibility.
+
+ ftp, http and https streams are handled separately.
+
*/
static MagickBooleanType MW_read_image( MagickWand *magick_wand, const char *format_or_file )
{
@@ -115,8 +118,51 @@
TSRMLS_FETCH();
real_filename[0] = '\0';
+
+ /* Special check for ftp, http and https */
+ if (strlen(format_or_file) >= 6) {
+ if (strncasecmp(format_or_file, "ftp://", 6) == 0 ||
+ strncasecmp(format_or_file, "http://", 7) == 0 ||
+ strncasecmp(format_or_file, "https://", 8) == 0) {
+ FILE *fp;
+ php_stream *stream = php_stream_open_wrapper((char *)format_or_file, "rb", REPORT_ERRORS | ENFORCE_SAFE_MODE, NULL);
+
+ if (!stream) {
+ /* No stream, fail here */
+ return MagickFalse;
+ }
+
+ if (php_stream_can_cast(stream, PHP_STREAM_AS_STDIO | PHP_STREAM_CAST_INTERNAL) == FAILURE) {
+ php_stream_close(stream);
+ return MagickFalse;
+ }
+ if (php_stream_cast(stream, PHP_STREAM_AS_STDIO | PHP_STREAM_CAST_INTERNAL, (void*)&fp, 0) == FAILURE) {
+ php_stream_close(stream);
+ return MagickFalse;
+ }
+ if (MagickReadImageFile(magick_wand, fp)) {
+ unsigned long orig_img_idx = (unsigned long) MagickGetNumberImages( magick_wand );
+ php_stream_close(stream);
+
+ if ( MagickSetImageIndex( magick_wand, (long) orig_img_idx ) == MagickTrue ) {
+ while ( MagickSetImageFilename( magick_wand, (char *) NULL ), MagickNextImage( magick_wand ) == MagickTrue )
+ ;
+ }
+ MagickClearException( magick_wand );
+ MagickResetIterator( magick_wand );
+ return MagickTrue;
+ } else {
+ php_stream_close(stream);
+ MW_API_FUNC_FAIL_CHECK_WAND_ERROR_EX_1( magick_wand, MagickWand,
+ "cannot read the format \"%s\"",
+ format_or_file );
+ return MagickFalse;
+ }
+ }
+ }
+
#define PRV_VERIFY_MAGICK_FORMAT_FILE( magick_filename, colon_p, is_magick_format ) \
{ \
magick_filename = colon_p + 1; \
Code: Select all
<?php
$wand = NewMagickWand();
MagickReadImage($wand, "http://example.com/test.png");