handling svg images php

2.7k views Asked by At

I have an image function for handling various image types. Now I need to include svg images also. I would like to know functions like imagecreatefromgif($image) for svg images,something like imagecreatefromsvg($svgimage).

Also I would like to know equivalent function like imagegif($Newimage,$image) for svg images.

function resizeImage($image,$width,$height,$scale) {

list($imagewidth, $imageheight, $imageType) = getimagesize($image);
$imageType = image_type_to_mime_type($imageType);
$newImageWidth = ceil($width * $scale);
$newImageHeight = ceil($height * $scale);
$newImage = imagecreatetruecolor($newImageWidth,$newImageHeight);
switch($imageType) {
    case "image/gif":
        $source=imagecreatefromgif($image); 
        break;
    case "image/pjpeg":
    case "image/jpeg":
    case "image/jpg":
        $source=imagecreatefromjpeg($image); 
        break;
    case "image/png":
    case "image/x-png":
        $source=imagecreatefrompng($image); 
        break;
   **for SVG**
     //case "image/svg-xml":
     //$source=imagecreatefromsvg($image); 
     //   break;**
  }
imagecopyresampled($newImage,$source,0,0,0,0,$newImageWidth,$newImageHeight,$width,$height);

switch($imageType) {
    case "image/gif":
          imagegif($newImage,$image); 
        break;
      case "image/pjpeg":
    case "image/jpeg":
    case "image/jpg":
          imagejpeg($newImage,$image,90); 
        break;
    case "image/png":
    case "image/x-png":
        imagepng($newImage,$image);  
        break;
   **for SVG**
     //case "image/svg-xml":
     //imagepng($newImage,$image); 
     //   break;**
}

chmod($image, 0777);
return $image;

}

1

There are 1 answers

0
Mikhail Pukas On

Convert to PNG

$im = new Imagick();
$svg = file_get_contents($link);
$im->readImageBlob($svg);
$im->setImageFormat("png24");
$im->writeImage($image);
$im->clear();
$im->destroy();