使用PHPPowerPoint调整幻灯片内的图像大小

时间:2018-03-27 19:20:30

标签: php phppowerpoint

我正在使用PHPPowerPoint lib生成一些powerpoint文件。问题是来自服务器的图像太大,并且打破了幻灯片的限制。 这是我用于图像的一些代码:

if(file_exists($dir.$image)){
    // Add logo
    $shape = $slide->createDrawingShape();
    $shape->setName('PHPPowerPoint logo');
    $shape->setDescription('PHPPowerPoint logo');
    $shape->setPath($dir.$image);
    $shape->setWidth(310);
    $shape->setHeight(608);
    $shape->setOffsetX(330);  
    $shape->setOffsetY(70); 
    //$shape->setOffsetY(720 - 10 - 40);
    }

请注意,我已经设置了宽度和高度,但图像不符合这些配置。我能做些什么来使图像尊重我需要的尺寸或获得响应行为?

修改: 这是函数createTemplateSlide的代码:

function createTemplatedSlide(PHPPowerPoint $objPHPPowerPoint, $dir, $image, $razao, $nome, $data, $contador)
{
    // Create slide
    $slide = $objPHPPowerPoint->createSlide();

    if(file_exists($dir.$image)){
    // echo("$contador) o arquivo: '".$dir.$image."' existe <br>");
    // Add logo
    $shape = $slide->createDrawingShape();
    $shape->setName('PHPPowerPoint logo');
    $shape->setDescription('PHPPowerPoint logo');
    $shape->setPath($dir.$image);
    $shape->setWidth(310);
    $shape->setHeight(608);
    $shape->setOffsetX(330);  
    $shape->setOffsetY(70); 
    //$shape->setOffsetY(720 - 10 - 40);
    }

    // Create a shape (text)
    // echo date('H:i:s') . " Create a shape (rich text)<br>";
    $shape = $slide->createRichTextShape();
    $shape->setHeight(60);
    $shape->setWidth(960);
    $shape->setOffsetX(0);
    $shape->setOffsetY(20);
    $shape->getAlignment()->setHorizontal( PHPPowerPoint_Style_Alignment::HORIZONTAL_CENTER );

    $textRun = $shape->createTextRun($razao."  ".$data);
    $textRun->getFont()->setBold(true);
    $textRun->getFont()->setSize(16);
    $textRun->getFont()->setColor( new PHPPowerPoint_Style_Color( '00000000' ) );

    for ($i=0; $i <23 ; $i++) { 
        $shape->createBreak();
    }

    $textRun = $shape->createTextRun(utf8_encode($nome));
    $textRun->getFont()->setBold(true);
    $textRun->getFont()->setSize(16);
    $textRun->getFont()->setColor( new PHPPowerPoint_Style_Color( '00000000' ) );


    // Return slide
    return $slide;
}

1 个答案:

答案 0 :(得分:0)

根据@MadeInDreams给出的建议,我找到了这个主题:

Resize image in PHP

我使用了那里的解决方案和 imagejpeg 功能将新生成的图像复制到我项目的某个文件夹中。