Freelancer Martin Zeller

php asp.net java c# .net c++ xml xhtml seo magento zend framework dotnetnuke tomcat iis sql server mysql oracle typo3 coremedia

Pdf thumbnails / Pdf to jpg

Tags: ,

Do you want to create thumbnails for pdf files with PHP (or C#)? You can simply do this with ghostscript. Here’s the php way (it is very easy to convert this code to C#):

  1. First install ghostscript on your server. Download: http://ghostscript.com/releases/
  2. Then write your code:
$pathToPdf = "C:\....\PdfFile.pdf";
$pathToJpg = "C:\....\generatedThumbnail.jpg";

$gsCall = "\"C:\....\gs\gs8.70\bin\gswin32.exe\" -q -dBATCH -dMaxBitmap=300000000 -dNOPAUSE -dSAFER -sDEVICE=jpeg -dTextAlphaBits=4 -dGraphicsAlphaBits=4 -dFirstPage=1 -dLastPage=1 -sOutputFile={0} {1} -c quit";

$gsCall = str_replace(array('{0}', '{1}'), array($pathToJpg, $pathToPdf), $gsCall); // or use sprintf
$str = exec($gsCall, $output=array(), $returnValue);

This code will generate a jpg file with the content of the first page of the pdf file.

Ready.

Zend_Captcha, an example without Zend_Form

Tags: ,

Here is your example how to implement Zend_Captcha_Image without using Zend_Form.

In your action method:

public function indexAction()
{
//...
$captcha = new Zend_Captcha_Image();
$captcha->setImgDir(APPLICATION_PATH . '/../public/tmp/captcha/');
$captcha->setImgUrl($this->view->baseUrl('/tmp/captcha/'));
$captcha->setFont(APPLICATION_PATH . '/fonts/elephant.ttf');
$captcha->setWidth(350);
$captcha->setHeight(150);
$captcha->setWordlen(5);
$captcha->setFontSize(70);
$captcha->setLineNoiseLevel(3);
$captcha->generate();
$this->view->captcha = $captcha;   // giving captcha object to the view
//...
}

In the corresponding view render the captcha and add a hidden field with the captcha id:

<form>
<input id="captcha" type="text" name="captcha" />
<?php echo $this->captcha->render($this, null) ?>
<input type="hidden" name="cid" value="<?php echo $this->captcha->getId() ?>" >
</form>

Validating the input of the user after postback:

// ...
$capId = $_POST['cid'];
$capSession = new Zend_Session_Namespace('Zend_Form_Captcha_'.$capId);
if ($_POST['captcha']==$capSession->word)
{
  // input OK
}
else {
  // input NOK
}
// ...

Good luck!

PS: If you want to use your Zend_Captcha WITH Zend_Form, this blog post may help you: Zend_Captcha with Zend_Form

PHP – Delete files by pattern

Tags:

If you want to delete files by pattern (e.g. “*.txt”) in a directory, you can do this with the php function glob!

$files = glob('/path/to/files/*.txt'); // finds all txt files in the directory /path/to/files/
foreach($files as $file)
  unlink($file);

With this method you can also delete all files of a directory – with pattern *.*

PHP – Delete all files in a directory

Tags:

Simple question – simple answer:

How do I delete all files in a directory with PHP?

$files = glob('/path/to/directory/*.*');
foreach($files as $file)
  unlink($file);

Client localisation with zend framework

Tags: ,

If you want to find out the language or country of the clients browser, do it this way:

<?php
require_once 'Zend/Locale.php';
$locale = new Zend_Locale();
echo $locale->toString();
?>

But be aware! This way you only get the localisation of the clients browser. And this is configurable by the user.

The code above will give you output like this:

de_AT

or

en_US

There are many methods you can use with zend localisation:

<?php echo $locale->toString() ?>
<?php echo $locale->getLanguage() ?>
<?php echo $locale->getRegion() ?>
<?php print_r($locale->getBrowser()); // Return an array of all accepted languages of the client ?>

Example output:

de_AT
de
AT
Array (     [de_AT] => 1     [de] => 1     [de_DE] => 0.8     [en_GB] => 0.5     [en] => 0.5     [en_US] => 0.3 )

Good luck!

Magento: Warning in Mage/Eav/Model/Entity/Abstract.php ?

Tags: ,

All you want to do is saving a product like this?

$api = new Mage_Catalog_Model_Product_Api();
$productData = array();
$productData['visibility'] = Mage_Catalog_Model_Product_Visibility::VISIBILITY_NOT_VISIBLE;
$productData['status'] = Mage_Catalog_Model_Product_Status::STATUS_DISABLED;
$api->update($product->getId(), $productData);

Or maybe without the API?

$product->setVisibility(Mage_Catalog_Model_Product_Visibility::VISIBILITY_NOT_VISIBLE);
$product->setStatus(Mage_Catalog_Model_Product_Status::STATUS_DISABLED);
$product->save();

And all you get is an error like this?

Warning: Invalid argument supplied for foreach() in /opt/lampp/htdocs/whlid/app/code/core/Mage/Eav/Model/Entity/Abstract.php on line 972

#0 /opt/lampp/htdocs/whlid/app/code/core/Mage/Eav/Model/Entity/Abstract.php(972): mageCoreErrorHandler(2, 'Invalid argumen...', '/opt/lampp/htdo...', 972, Array)
#1 /opt/lampp/htdocs/whlid/app/code/core/Mage/Eav/Model/Entity/Abstract.php(927): Mage_Eav_Model_Entity_Abstract-&gt;_collectSaveData(Object(Mage_Catalog_Model_Product))
#2 /opt/lampp/htdocs/whlid/app/code/core/Mage/Core/Model/Abstract.php(251): Mage_Eav_Model_Entity_Abstract-&gt;save(Object(Mage_Catalog_Model_Product))
#3 /opt/lampp/htdocs/whlid/app/code/core/Mage/Catalog/Model/Product/Api.php(219): Mage_Core_Model_Abstract-&gt;save()
#4 /opt/lampp/htdocs/whlid/app/code/local/Willhaben/Customer/controllers/AccountController.php(20): Mage_Catalog_Model_Product_Api-&gt;update('161', Array)
#5 /opt/lampp/htdocs/whlid/app/code/core/Mage/Core/Controller/Varien/Action.php(376): Willhaben_Customer_AccountController-&gt;deleteanzeigeAction()
#6 /opt/lampp/htdocs/whlid/app/code/core/Mage/Core/Controller/Varien/Router/Standard.php(248): Mage_Core_Controller_Varien_Action-&gt;dispatch('deleteanzeige')
#7 /opt/lampp/htdocs/whlid/app/code/core/Mage/Core/Controller/Varien/Front.php(158): Mage_Core_Controller_Varien_Router_Standard-&gt;match(Object(Mage_Core_Controller_Request_Http))
#8 /opt/lampp/htdocs/whlid/app/Mage.php(459): Mage_Core_Controller_Varien_Front-&gt;dispatch()
#9 /opt/lampp/htdocs/whlid/index.php(65): Mage::run()
#10 {main}

The error occurs in this area of /app/code/core/Mage/Eav/Model/Entity/Abstract.php ?

$origData = $this->_getOrigObject($newObject)->getOrigData();

/**
* drop attributes that are unknown in new data
* not needed after introduction of partial entity loading
*/
foreach ($origData as $k=>$v) {
if (!array_key_exists($k, $newData)) {
unset($origData[$k]);
}
}

SOLUTION: try to prepend this line to your code:

Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);

Hallo Welt!

Tags: , ,

Meine alte Homepage, die ich mit typo3 umgesetzt hatte, gehört der Vergangenheit an. Diese Version wurde mit WordPress 2.8 erstellt. Funktioniert tadellos.

© 2009 Freelancer Martin Zeller. All Rights Reserved.

This blog is powered by Wordpress and Magatheme by Bryan Helmig.