Skip to content

Instantly share code, notes, and snippets.

@putzflorian
Last active April 3, 2024 13:45
Show Gist options
  • Select an option

  • Save putzflorian/5372476 to your computer and use it in GitHub Desktop.

Select an option

Save putzflorian/5372476 to your computer and use it in GitHub Desktop.
Website_Tool_Helper
<?php
class Website_Tool_Helper
{
public static function getImageTitle($image, $language)
{
$key = 'bild_title_' . $language;
$title = $image->getProperty($key);
return $title;
}
public static function getImageDescription($image, $language)
{
$key = 'bild_description_' . $language;
$description = $image->getProperty($key);
return $description;
}
public static function getFirstImage($stack)
{
if($stack[0] instanceof Asset_Image){
return $stack[0];
} else {
if($stack[0] instanceof Asset_Folder){
$childs = $stack[0]->getChilds();
return $childs[0];
}
}
}
public static function getTimestampBeginnofDay($d = null)
{
if($d){
$date = new Zend_Date($d);
} else {
$date = new Zend_Date();
}
$date->setHour(0);
$date->setMinute(0);
$date->setSecond(0);
return $date;
}
public static function getTimestampEndofDay($d = null)
{
if($d){
$date = new Zend_Date($d);
} else {
$date = new Zend_Date();
}
$date->setHour(23);
$date->setMinute(50);
$date->setSecond(59);
return $date;
}
public static function findNiceUrl($link)
{
$pos = strpos($link, 'http://');
$pos1 = strpos($link, 'https://');
if ($pos !== false || $pos1 !== false) {
$newlink = $link;
} else {
$newlink = 'http://' . $link;
}
return $newlink;
}
public static function getPdfImage($file,$width,$height)
{
$imageFile = PIMCORE_TEMPORARY_DIRECTORY . "/pdf_preview-" . md5($file . $width . $height) . ".jpg";
if(!file_exists($imageFile)) {
$im = new imagick( PIMCORE_ASSET_DIRECTORY . $file .'[0]' );
$im->setImageColorspace(255);
$im->setCompression(Imagick::COMPRESSION_JPEG);
$im->setCompressionQuality(90);
$im->setImageFormat('jpeg');
$im->resizeImage($width, $height, imagick::FILTER_LANCZOS, 1);
$im->writeImage( $imageFile );
}
return str_replace(PIMCORE_DOCUMENT_ROOT, "", $imageFile);
}
public static function getExternamImg($path, $filename, $width, $height)
{
if(!is_dir(PIMCORE_TEMPORARY_DIRECTORY . '/manualimages')){
mkdir(PIMCORE_TEMPORARY_DIRECTORY . '/manualimages', 0755);
}
$dimension = $width . '_' . $height . '_';
$mykey = $dimension . $path . md5(time()) . ".jpg";
$targetfile = '/website/var/tmp/manualimages/' . Pimcore_File::getValidFilename($dimension . $filename);
$destination = PIMCORE_TEMPORARY_DIRECTORY . '/manualimages/' . Pimcore_File::getValidFilename($dimension . $filename);
if(is_file($destination)) {
// older than 24 hours
$generatetime = time() - 86400;
if($generatetime > filemtime($destination)){
self::grabImage($mykey, $path, $width, $height, $destination);
}
} else {
self::grabImage($mykey, $path, $width, $height, $destination);
}
return $targetfile;
}
private static function grabImage($mykey, $path, $width, $height, $destination){
try {
$tmpFile = PIMCORE_TEMPORARY_DIRECTORY . "/tmp_" . Pimcore_File::getValidFilename($mykey);
file_put_contents($tmpFile, Pimcore_Tool::getHttpData($path));
$image = Pimcore_Image::getInstance();
$image->load($tmpFile);
$image->cover($width, $height, "center");
$image->save($destination, "jpg", 85);
unlink($tmpFile);
} catch (Exception $e) {
return false;
}
}
public static function getImages($stack){
$imagearray = array();
foreach($stack as $img){
if($img instanceof Asset_Image){
$imagearray[] = $img;
}
if($img instanceof Asset_Folder){
foreach($img->getChilds() as $childs){
if($childs instanceof Asset_Image){
$imagearray[] = $childs;
}
}
}
if($img instanceof Document_Snippet){
$imagearray[] = $img;
}
}
return $imagearray;
}
public static function create_zip($files = array(),$destination = '',$overwrite = false) {
//if the zip file already exists and overwrite is false, return false
if(file_exists($destination) && !$overwrite) { return false; }
//vars
$valid_files = array();
//if files were passed in...
if(is_array($files)) {
//cycle through each file
foreach($files as $file) {
//make sure the file exists
if(file_exists($file)) {
$valid_files[] = $file;
}
}
}
//if we have good files...
if(count($valid_files)) {
//create the archive
$zip = new ZipArchive();
if($zip->open($destination,$overwrite ? ZIPARCHIVE::OVERWRITE : ZIPARCHIVE::CREATE) !== true) {
return false;
}
//add the files
foreach($valid_files as $file) {
$tmp = explode('/', $file);
$zip->addFile($file,$tmp[count($tmp) -1]);
}
//debug
//echo 'The zip archive contains ',$zip->numFiles,' files with a status of ',$zip->status;
//close the zip -- done!
$zip->close();
//check to make sure the file exists
return file_exists($destination);
}
else
{
return false;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment