Is this way the best solution to read and save images from Rss Feeds?
Posted
by Keyne
on Stack Overflow
See other posts from Stack Overflow
or by Keyne
Published on 2010-04-20T01:21:35Z
Indexed on
2010/04/20
1:33 UTC
Read the original article
Hit count: 345
Hi, I've build a "RSS Feed Reader" module to my application, and for every post with images in the content, I get the first that matches a specific size and then I save for future use as reference to the posts.
When processing, everything works properly except when more then 10 images are saved at the same time (This value is variable). I got an 404 error after a while saving the images. Some images are saved, but not all of them. If I run the app in my local server, everything work as expected (using set_timeout(0) to avoid timeout errors).
Currently my application is hosted at dreamhost and I'm using Zend Framework. I'm not sure if the problem is specif for Zend Framework applications, so I didn't put any tags about. I've had this problem another time with other projects hosted at dreamhost. They seems to limitate memory for operations like that.
My question is: There is a better way to save images then this (maybe in background? a limited amount per operation?):
require_once(APPLICATION_PATH . '/../library/simplehtmldom/simple_html_dom.php');
$TableFeeds = new Model_DbTable_Feeds();
$Feeds = $TableFeeds->fetchAll();
foreach($Feeds as $Feed)
{
$entries = Zend_Feed_Reader::import($Feed->link);
$lastEntry = $this->getLastEntry($Feed->id);
if($lastEntry)
{
$lastDate = new Zend_Date($lastEntry->created_at, Zend_Date::ISO_8601);
}
foreach ($entries as $entry) {
if($lastDate)
{
if(strtotime($lastDate->get('y-M-d H:m:s')) >= strtotime($entry->getDateCreated()->get('y-M-d H:m:s')))
{
break;
}
}
$Category = new Model_DbTable_Categorias();
$availableCategories = $Category->getList('slug');
$categoria_id = false;
foreach ($categories as $cat) {
if(!empty($cat) && !$categoria_id)
{
$slug = Zf_Convert::slugfy($cat);
if($availableCategories[$Feed->categoria_id] == $slug)
{
$categoria_id = $Feed->categoria_id;
break;
}
if(in_array($slug,$availableCategories))
{
$categoria_id = array_search($slug,$availableCategories);
break;
}
}
}
if(!$categoria_id)
{
$categoria_id = $Feed->categoria_id;
}
// Cadastra a entrada
$data = array(
'categoria_id' => $categoria_id,
'feed_id' => $Feed->id,
'titulo' => $entry->getTitle(),
'slug' => Zf_Convert::slugfy($entry->getTitle()),
'created_at' => $entry->getDateCreated()->get(Zend_Date::ISO_8601),
'link' => $entry->getLink(),
'html' => $entry->getContent(),
'tags' => $tags
);
$entry_id = $this->insert($data);
if($categoria_id == 2)
{
set_time_limit(0);
$post_dom = str_get_dom($entry->getContent());
$img_tags = $post_dom->find('img');
$images = array();
foreach($img_tags as $image) {
$images[] = $image->src;
}
if(count($images)) {
$i = 0;
$saved = false;
while($images[$i] && !$saved)
{
$src = $images[$i];
$imagem = Zf_Image::create($src);
$size = $imagem->getCurrentDimensions();
if($size['width'] >= 150 && $size['height'] >= 200)
{
$imagem->adaptiveResize(200,200);
$imagem->save(PUBLIC_PATH . '/img/feeds/manchetes/' . $entry_id . '.jpg');
$saved = true;
$this->update(array('imagemChamada' => 1),'id =' . $entry_id);
}
$i++;
}
}
}
}
}
}
© Stack Overflow or respective owner