我使用Imagick打开图像,调整图像大小,设置图像格式(如果需要)并写入另一个图像格式 文件夹结果。 它适用于ICO和PNG,但无法编写SVG </ strong>文件,说</ p>

ImagickException:无法写入文件:C:\ Sites Imagick中的\ devdesktop \ test \ sites \ default \ files \ tempimages \ abstract-spiral-striped-background-100.svg-> writeimage()(C:\ Sites \ devdesktop \ test \ sites \ all \ modules的第44行) 这是我的代码:</ p p>

  $ file = file_load($ form_state ['values'] ['fid']); 
$ image = new Imagick(drupal_realpath($ file-&gt; uri));

$ image-&gt; setImageFormat($ type [$ type_num]);
$ image-&gt; resizeImage($ size [$ size_num],$ size [$ size_num],Imagick :: FILTER_UNDEFINED,1);
$ destination ='public://tempimages/'.substr($ file-&gt; filename,0,-4).'-'。$ size [$ size_num]。'。'。$ type [$ type_num]; \ N $图像 - &GT; writeImage(drupal_realpath($目的地)); //<---problem
drupal_goto(base_path().'/sites/default/files/tempimages/'.substr($file->filename,0,-4).'-'。$ size [$ size_num]。'。'。$ type [$ type_num]);
</ code> </ pre>

此代码段与ICO和PNG完美配合,但不写入文件 SVG格式除了一点点代码和平</ p>

 &lt;?xml version =“1.0”standalone =“no”?&gt; 
&lt;!DOCTYPE svg PUBLIC“ - // W3C // DTD SVG 20010904 // EN“">
<svg width =“200” height =“200”&gt;
&lt; g style =“&lt; / svg&gt;
</ code> </ pre>

为什么会这样?</ p>
</ div>



I use Imagick to open an image, resize it, set an image format (if it's needed) and write in another folder the result. It works with ICO and PNG, but can't write SVG files, saying that

ImagickException: Unable to write the file: C:\Sites\devdesktop\test\sites\default\files\tempimages\abstract-spiral-striped-background-100.svg in Imagick->writeimage() (line 44 of C:\Sites\devdesktop\test\sites\all\modules\testmodule\testmodule.module).

It was an error message from Drupal 7.

Here is my code:

$file = file_load($form_state['values']['fid']);
$image = new Imagick(drupal_realpath($file->uri));
$image->resizeImage($size[$size_num], $size[$size_num], Imagick::FILTER_UNDEFINED, 1);
$destination = 'public://tempimages/'.substr($file->filename, 0, -4).'-'.$size[$size_num].'.'.$type[$type_num];
$image->writeImage(drupal_realpath($destination));    //<---problem
drupal_goto(base_path().'/sites/default/files/tempimages/'.substr($file->filename, 0, -4).'-'.$size[$size_num].'.'.$type[$type_num]);

This code snippet works perfectly with ICO and PNG, but don't write a file in SVG format except a little peace of code

<?xml version="1.0" standalone="no"?>
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 20010904//EN"
<svg width="200" height="200">
<g style="</svg> 

Why so?


SVG </ code>不是与 JPG </ code相同的图像 >或 PNG </ code>是。</ strong> </ p>

在大多数情况下,它只是一个带有绘制点的 xml </ code>,通常没有 尺寸和分辨率。</ p>

200 </ code>的 width </ code>和 height </ code>只是中的大小 保存它的px </ code>。 所以你永远不需要'调整'svg,因为它会无限扩展。</ p>

如果你把 SVG </ code>放在 img </ code中 >标记并定义尺寸,例如:</ p>

&lt; img src =“myawesome.svg”width =“500”height =“500”/&gt; </ code> </ p>

即使它在 SVG </ code>中定义为具有 200 </ code>的维度,它也应该扩大或缩小,而不会改变 外观质量。</ p>

由于结构完全不同,在某些情况下它不会遵循与 jpg </ code>或 png </ code相同的规则 >将它们渲染为图像。</ p>

如果在Drupal中完成此操作; 为 SVG </ code>做一个例外,它只存储文件而不调整大小,并为所有图像添加打印尺寸。 它不会伤害渲染的图像,它应该将 SVG </ code>显示为与它们相同的大小。</ p>

TL; DR </ strong> SVG </ code>是一个 xml </ code>文件,不是真正的图像,调整它是没有意义的,因为它没有真正的分辨率或大小,可以无限扩展。 它们看起来很相似,因为它们都是视觉元素。 为 SVG </ code>做一个例外,因此它只存储文件,当打印渲染图像或 SVG </ code>时,将调整大小的尺寸添加到 img </ 代码>输出。</ p>
</ div>



SVG isn't an image in the same way that JPG or PNG are.

It's just an xml with plotted points in most cases, and is typically void of dimension and resolution.

The width and height of 200 are just the size in px at which it was saved. So you should never need to 'resize' an svg, as it will scale infinitely.

If you put an SVG in an img tag and define the dimensions, like:

<img src="myawesome.svg" width="500" height="500" />

Even though it's defined in the SVG as having a dimensions of 200, it should scale larger or smaller with no change in the quality of appearance.

Because of this complete difference in structure, it will not follow the same rules in some cases as jpg or png will as they are rendered images.

If this is being done in Drupal; make an exception for SVG that just stores the file without resizing, and add the dimensions where ever it is printed for all images. It won't hurt the rendered images, and it should show the SVG as the same size as them.

TL;DR SVG is an xml file, not really an image, resizing it is pointless since it carries no real resolution or size and can scale infinitely. They only appear to be similar since they are both visual elements. Make an exception for SVG so it just stores the file, and when either the rendered images or the SVG is printed, add the resized dimensions to the img output.

Csdn user default icon