note 125037 added to imagick.identifyimage

From: Date: Sat, 23 May 2020 03:41:30 +0000
Subject: note 125037 added to imagick.identifyimage
Groups: php.notes 
Request: Send a blank email to php-notes+get-214664@lists.php.net to get a copy of this message
I have created a function to parse the "rawOutput" because the information provided by this function is quite useless. function parseIdentify($info) { $lines = explode("\n", $info); $outputs = []; $output = []; $keys = []; $currSpaces = 0; $raw = false; foreach($lines as $line) { $trimLine = trim($line); if(empty($trimLine)) continue; if($raw) { preg_match('/^[0-9]+:\s/', $trimLine, $match); if(!empty($match)) { $regex = '/([\d]+):'; $regex .= '\s(\([\d|\s]{1,3},[\d|\s]{1,3},[\d|\s]{1,3},[\d|\s]{1,3}\))'; $regex .= '\s(#\w+)'; $regex .= '\s(srgba\([\d|\s]{1,3},[\d|\s]{1,3},[\d|\s]{1,3},[\d|\s|.]+\)|\w+)/'; preg_match($regex, $trimLine, $matches); array_shift($matches); $output['Image'][$raw][] = $matches; continue; } else { $raw = false; array_pop($keys); } } preg_match('/^\s+/', $line, $match); $spaces = isset($match[0]) ? strlen($match[0]) : $spaces = 0; $parts = preg_split('/:\s/', $trimLine, 2); $_key = ucwords($parts[0]); $_key = str_replace(' ', '', $_key); $_val = isset($parts[1]) ? $parts[1] : []; if($_key == 'Image') { if(!empty($output)) { $outputs[] = $output['Image']; $output = []; } $_val = []; } if($spaces < $currSpaces) { for($i = 0; $i < ($currSpaces - $spaces) / 2; $i++) { array_pop($keys); } } if(is_array($_val)) { $_key = rtrim($_key, ':'); $keys[] = $_key; if($_key == 'Histogram' || $_key == 'Colormap') { $raw = $_key; } } $currSpaces = $spaces; $arr = &$output; foreach($keys as $key) { if(!isset($arr[$key])) { $arr[$key] = $_val; } $arr = &$arr[$key]; } if(!is_array($_val)) { $arr[$_key] = $_val; } } $outputs[] = $output['Image']; return count($outputs) > 1 ? $outputs : $outputs[0]; } Usage example: $img = new Imagick('example.png'); $identify = parseIdentify($img->identifyImage(true)['rawOutput']); But the "rawOutput" that this function returns only contains the first frame if it is a GIF. Alternatively you can use the command "identify" to get the data of all the frames: $identify = parseIdentify(shell_exec('identify -verbose example.gif')); ---- Server IP: 45.112.84.4 Probable Submitter: 103.51.166.36 (proxied: 213.195.112.77) ---- Manual Page -- http://php.net/manual/en/imagick.identifyimage.php Edit -- https://master.php.net/note/edit/125037 Del: integrated -- https://master.php.net/note/delete/125037/integrated Del: useless -- https://master.php.net/note/delete/125037/useless Del: bad code -- https://master.php.net/note/delete/125037/bad+code Del: spam -- https://master.php.net/note/delete/125037/spam Del: non-english -- https://master.php.net/note/delete/125037/non-english Del: in docs -- https://master.php.net/note/delete/125037/in+docs Del: other reasons-- https://master.php.net/note/delete/125037 Reject -- https://master.php.net/note/reject/125037 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#214664) next »