英文:
Parsing PPTX in PHP, Need to find coordinates to attached images
问题
我正在尝试获取通过PHP上传的PPTX文件中上传的图像的X、Y坐标。由于我们在共享服务器上,所以受到模块使用的限制。
我找不到任何地方可以找到任何信息。我尝试在社区中搜索,但无法找到解决方案。
任何帮助将不胜感激。
function pptx_extract_images() {
// 获取提交的数据
$data = filter_input_array(INPUT_POST);
$attachment_id = $data['attachment_id'];
// 获取附件文件及其名称
$input_file = get_attached_file($attachment_id);
$input_file_name = pathinfo($input_file)["filename"];
// 获取附件类型
$attachment_type = pathinfo($input_file, PATHINFO_EXTENSION);
// 创建pptx的zip压缩包
$package = new \ZipArchive();
// 如果不是zip/pptx文件
if (!isset($package)) {
return;
}
$package->open($input_file);
// 读取关系并搜索图像
$relationsXml = $package->getFromName('_rels/.rels');
if ($relationsXml === false) {
$logger->write_log('pptx_extract_images', $relationsXml, '无效的归档或损坏的.pptx文件。');
throw new RuntimeException('无效的归档或损坏的.pptx文件。');
}
$relations = simplexml_load_string($relationsXml);
function absoluteZipPath($path) {
$path = str_replace(array('/', '\\'), DIRECTORY_SEPARATOR, $path);
$parts = array_filter(explode(DIRECTORY_SEPARATOR, $path), 'strlen');
$absolutes = array();
foreach ($parts as $part) {
if ('.' == $part) continue;
if ('..' == $part) {
array_pop($absolutes);
} else {
$absolutes[] = $part;
}
}
return implode('/', $absolutes);
}
// 文档数据容器
$slides = 0;
$data = array();
foreach ($relations->Relationship as $rel) {
if ($rel["Type"] == 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument') {
$slideRelations = simplexml_load_string($package->getFromName(absoluteZipPath(dirname($rel["Target"]) . "/_rels/" . basename($rel["Target"]) . ".rels")));
foreach ($slideRelations->Relationship as $slideRel) {
if ($slideRel["Type"] == 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/slide') {
$slideNotesRelations = simplexml_load_string($package->getFromName(absoluteZipPath(dirname($rel["Target"]) . "/" . dirname($slideRel["Target"]) . "/_rels/" . basename($slideRel["Target"]) . ".rels")));
$slideNo = isset($slideRel["Target"]) ? str_replace('slide', '', pathinfo($slideRel["Target"])["filename"]) : null;
foreach ($slideNotesRelations->Relationship as $slideImageRel) {
if ($slideImageRel["Type"] == 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/image') {
$image = basename($slideImageRel["Target"]);
$image_mime = explode('.', $image);
$count_explode = count($image_mime);
$image_mime = strtolower($image_mime[$count_explode - 1]);
if ($image_mime == 'gif') {
$data['page_' . ($slideNo - 1)]['animated_image'] = $attachment_id . '-' . $input_file_name . '/image-' . $image;
}
}
}
$slides++;
}
}
}
}
$package->close();
}
感谢您查看,非常感谢。
英文:
I am trying to get the X,Y coordinates of images uploaded to the PPTX file uploaded using PHP.
We are on a shared server, so limited by module usage.
There is no place where i could find anything. I tried searching the community, but unable to find a solution.
Any help would be appreciated.
function pptx_extract_images() {
// Getting post data
$data = filter_input_array(INPUT_POST);
$attachment_id = $data['attachment_id'];
// Getting attachment file and its name
$input_file = get_attached_file($attachment_id);
$input_file_name = pathinfo($input_file)["filename"];
// Getting attachment type
$attachment_type = pathinfo($input_file, PATHINFO_EXTENSION);
// Making a zip archive package for pptx
$package = new \ZipArchive();
//if not a zip/pptx file
if (!isset($package)) {
return;
}
$package->open($input_file);
// Read relations and search for images
$relationsXml = $package->getFromName('_rels/.rels');
if ($relationsXml === false) {
$logger->write_log('pptx_extract_images', $relationsXml, 'Invalid archive or corrupted .pptx file.');
throw new RuntimeException('Invalid archive or corrupted .pptx file.');
}
$relations = simplexml_load_string($relationsXml);
function absoluteZipPath($path) {
$path = str_replace(array('/', '\\'), DIRECTORY_SEPARATOR, $path);
$parts = array_filter(explode(DIRECTORY_SEPARATOR, $path), 'strlen');
$absolutes = array();
foreach ($parts as $part) {
if ('.' == $part) continue;
if ('..' == $part) {
array_pop($absolutes);
} else {
$absolutes[] = $part;
}
}
return implode('/', $absolutes);
}
// Document data holders
$slides = 0;
$data = array();
foreach ($relations->Relationship as $rel) {
if ($rel["Type"] == 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/officeDocument') {
$slideRelations = simplexml_load_string($package->getFromName(absoluteZipPath(dirname($rel["Target"]) . "/_rels/" . basename($rel["Target"]) . ".rels")));
foreach ($slideRelations->Relationship as $slideRel) {
if ($slideRel["Type"] == 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/slide') {
$slideNotesRelations = simplexml_load_string($package->getFromName(absoluteZipPath(dirname($rel["Target"]) . "/" . dirname($slideRel["Target"]) . "/_rels/" . basename($slideRel["Target"]) . ".rels")));
$slideNo = isset($slideRel["Target"]) ? str_replace('slide', '', pathinfo($slideRel["Target"])["filename"]) : null;
foreach ($slideNotesRelations->Relationship as $slideImageRel) {
if ($slideImageRel["Type"] == 'http://schemas.openxmlformats.org/officeDocument/2006/relationships/image') {
$image = basename($slideImageRel["Target"]);
$image_mime = explode('.', $image);
$count_explode = count($image_mime);
$image_mime = strtolower($image_mime[$count_explode - 1]);
if ($image_mime == 'gif') {
$data['page_' . ($slideNo - 1)]['animated_image'] = $attachment_id . '-' . $input_file_name . '/image-' . $image;
}
}
}
$slides++;
}
}
}
}
$package->close();
}
Thanks for looking into it, much appreciated.
答案1
得分: 1
这可以通过使用PHPPresentation库来实现。我注意到您提到了使用模块的困难,但在这里不应该成为问题,因为您可以下载发布版本并require
它们(无需使用composer)。虽然PHPPresentation文档有点不足,但它们的示例非常好,可以帮助您理解事物的运作方式。
以下是pptx的外观:
代码:
<?php
/*
问题作者:Ali Hussain
问题回答者:Jacob Mulquin
问题:在PHP中解析PPTX,需要找到附加图像的坐标
URL:https://stackoverflow.com/questions/76454701/parsing-pptx-in-php-need-to-find-coordinates-to-attached-images
标签:php, google-slides
*/
require_once 'PHPPresentation-1.0.0/src/PhpPresentation/Autoloader.php';
\PhpOffice\PhpPresentation\Autoloader::register();
require_once 'Common-1.0.1/src/Common/Autoloader.php';
\PhpOffice\Common\Autoloader::register();
$file = 'images.pptx';
$pptReader = PhpOffice\PhpPresentation\IOFactory::createReader('PowerPoint2007');
$oPHPPresentation = $pptReader->load($file);
function getShapeDetails($shape, $slide_number)
{
$width = $shape->getWidth();
$height = $shape->getHeight();
if ($width === 0 || $height === 0) {
return [];
}
$name = '';
$description = '';
if ($shape instanceof PhpOffice\PhpPresentation\Shape\Drawing\Gd) {
$name = $shape->getName();
$description = $shape->getDescription();
}
return [
'slide_number' => $slide_number+1,
'hashcode' => $shape->getHashCode(),
'offsetX' => $shape->getOffsetX(),
'offsetY' => $shape->getOffsetY(),
'width' => $width,
'height' => $height,
'name' => $name,
'description' => $description
];
}
$images = [];
foreach ($oPHPPresentation->getAllSlides() as $slide_number => $oSlide) {
foreach ($oSlide->getShapeCollection() as $oShape) {
if ($oShape instanceof PhpOffice\PhpPresentation\Shape\Group) {
foreach ($oShape->getShapeCollection() as $oShapeChild) {
$images[] = getShapeDetails($oShapeChild, $slide_number);
}
} else {
$images[] = getShapeDetails($oShape, $slide_number);
}
}
}
// 删除可能不是图像的形状
$images = array_values(array_filter($images));
var_dump($images);
产生的结果:
array(4) {
[0]=>
array(8) {
["slide_number"]=>
int(1)
["hashcode"]=>
string(32) "e2b4ed359604645d2e483f037ef81b55"
["offsetX"]=>
int(53)
["offsetY"]=>
int(19)
["width"]=>
int(468)
["height"]=>
int(236)
["name"]=>
string(9) "Picture 4"
["description"]=>
string(89) "A picture containing smile, yellow, smiley, emoticon
Description automatically generated"
}
[1]=>
array(8) {
["slide_number"]=>
int(1)
["hashcode"]=>
string(32) "6ea656b2a8426a6d6f2393391056842f"
["offsetX"]=>
int(925)
["offsetY"]=>
int(148)
["width"]=>
int(225)
["height"]=>
int(225)
["name"]=>
string(9) "Picture 6"
["description"]=>
string(83) "A picture containing design, font, logo, white
Description automatically generated"
}
[2]=>
array(8) {
["slide_number"]=>
int(2)
["hashcode"]=>
string(32) "3ddc522c60417b61b8a5f316b0f29dc2"
["offsetX"]=>
int(363)
["offsetY"]=>
int(235)
["width"]=>
int(402)
["height"]=>
int(269)
["name"]=>
string(21) "Content Placeholder 6"
["description"]=>
string(102) "A group of men playing baseball in a field
Description automatically generated with medium confidence"
}
[3]=>
array(8) {
["slide_number"]=>
int(3)
["hashcode"]=>
string(32) "cbefccdfc4db5e355f96bdc8d294296e"
["offsetX"]=>
int(365)
["offsetY"]=>
int(245)
["width"]=>
int(870)
["height"]=>
int(457)
["name"]=>
string(21) "Content Placeholder 4"
["description"]=>
string(90) "A picture containing text, font, screenshot, graphics
Description automatically generated"
}
}
英文:
This can be achieved using the PHPPresentation library. I note you mentioned difficulty with having modules, but that should not be an issue here as you can download the releases and require
them (no need for composer). While the PHPPresentation documentation is a bit lacking, their examples are really good to help figure out how things work.
Here is what the pptx looks like:
The code:
<?php
/*
Question Author: Ali Hussain
Question Answerer: Jacob Mulquin
Question: Parsing PPTX in PHP, Need to find coordinates to attached images
URL: https://stackoverflow.com/questions/76454701/parsing-pptx-in-php-need-to-find-coordinates-to-attached-images
Tags: php, google-slides
*/
require_once 'PHPPresentation-1.0.0/src/PhpPresentation/Autoloader.php';
\PhpOffice\PhpPresentation\Autoloader::register();
require_once 'Common-1.0.1/src/Common/Autoloader.php';
\PhpOffice\Common\Autoloader::register();
$file = 'images.pptx';
$pptReader = PhpOffice\PhpPresentation\IOFactory::createReader('PowerPoint2007');
$oPHPPresentation = $pptReader->load($file);
function getShapeDetails($shape, $slide_number)
{
$width = $shape->getWidth();
$height = $shape->getHeight();
if ($width === 0 || $height === 0) {
return [];
}
$name = '';
$description = '';
if ($shape instanceof PhpOffice\PhpPresentation\Shape\Drawing\Gd) {
$name = $shape->getName();
$description = $shape->getDescription();
}
return [
'slide_number' => $slide_number+1,
'hashcode' => $shape->getHashCode(),
'offsetX' => $shape->getOffsetX(),
'offsetY' => $shape->getOffsetY(),
'width' => $width,
'height' => $height,
'name' => $name,
'description' => $description
];
}
$images = [];
foreach ($oPHPPresentation->getAllSlides() as $slide_number => $oSlide) {
foreach ($oSlide->getShapeCollection() as $oShape) {
if ($oShape instanceof PhpOffice\PhpPresentation\Shape\Group) {
foreach ($oShape->getShapeCollection() as $oShapeChild) {
$images[] = getShapeDetails($oShapeChild, $slide_number);
}
} else {
$images[] = getShapeDetails($oShape, $slide_number);
}
}
}
// remove shapes that probably aren't images
$images = array_values(array_filter($images));
var_dump($images);
Yields:
array(4) {
[0]=>
array(8) {
["slide_number"]=>
int(1)
["hashcode"]=>
string(32) "e2b4ed359604645d2e483f037ef81b55"
["offsetX"]=>
int(53)
["offsetY"]=>
int(19)
["width"]=>
int(468)
["height"]=>
int(236)
["name"]=>
string(9) "Picture 4"
["description"]=>
string(89) "A picture containing smile, yellow, smiley, emoticon
Description automatically generated"
}
[1]=>
array(8) {
["slide_number"]=>
int(1)
["hashcode"]=>
string(32) "6ea656b2a8426a6d6f2393391056842f"
["offsetX"]=>
int(925)
["offsetY"]=>
int(148)
["width"]=>
int(225)
["height"]=>
int(225)
["name"]=>
string(9) "Picture 6"
["description"]=>
string(83) "A picture containing design, font, logo, white
Description automatically generated"
}
[2]=>
array(8) {
["slide_number"]=>
int(2)
["hashcode"]=>
string(32) "3ddc522c60417b61b8a5f316b0f29dc2"
["offsetX"]=>
int(363)
["offsetY"]=>
int(235)
["width"]=>
int(402)
["height"]=>
int(269)
["name"]=>
string(21) "Content Placeholder 6"
["description"]=>
string(102) "A group of men playing baseball in a field
Description automatically generated with medium confidence"
}
[3]=>
array(8) {
["slide_number"]=>
int(3)
["hashcode"]=>
string(32) "cbefccdfc4db5e355f96bdc8d294296e"
["offsetX"]=>
int(365)
["offsetY"]=>
int(245)
["width"]=>
int(870)
["height"]=>
int(457)
["name"]=>
string(21) "Content Placeholder 4"
["description"]=>
string(90) "A picture containing text, font, screenshot, graphics
Description automatically generated"
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论