I tweaked this function to get it working on my system.... Changes:

  • Added a function to create a path structure for the files if none exist
  • Removed one of the mime_retrieval methods as it didn;t work for me - though a PNG was a text/plain!
    • Here is the "new" function...

function imagecache_cache() {
  $generated = false;
  $args = func_get_args();
  $destination = file_create_path() . '/imagecache/' . implode('/', $args);
 	$dir = dirname($destination);
  
  if(!file_check_directory($dir)) {
  	$folders = explode("/", $dir);
  	$path = array();
  	
  	foreach($folders as $folder) {
  		$path[] = $folder;
  		if(!file_check_directory(implode("/", $path))) {
  			mkdir(implode("/", $path));
  		}
  	}
  }

  $function = array_shift($args);
	
  switch ($function) {
	  case 'resize':
	    $width = array_shift($args);
	    $height = array_shift($args);
	    $source = file_create_path(implode('/', $args));
	    if (!is_file($source)) { echo '$source does not exist'; }
	    if (image_resize($source, $destination, $width, $height)) $generated = true;
	    break;
	
	  case 'scale':
	    $width = array_shift($args);
	    $height = array_shift($args);
	    $source = file_create_path(implode('/', $args));
	    if (!is_file($source)) { echo '$source does not exist'; }
	    if (image_scale($source, $destination, $width, $height)) $generated = true;
	    break;
  }
  
  if ($generated)  {
		$size = getimagesize($destination);
		$mime = $size['mime'];

		file_transfer($destination, array('Content-Type: ' . mime_header_encode($mime), 'Content-Length: ' . filesize($destination)));
  }
}
CommentFileSizeAuthor
#4 imagecache.patch2.92 KBnicholasthompson

Comments

nicholasthompson’s picture

Just a follow up... I'm not sure why, but it seems to have lost quite a lot of its indentation! I can assue you, I dont code that way!!

dopry’s picture

Can you create a patch using diff? It makes it a lot easier for mesee exactly what changes you have made.

nicholasthompson’s picture

Believe me - I'd love to! *goes off to look up how to use Diff...*

nicholasthompson’s picture

StatusFileSize
new2.92 KB

I have attached a patch file. Is this what you were looking for?

kindafun’s picture

I'd love to use this thing but I can't figure out how to do so. Have installed it, put this patch on it, but I don't see any sign of it anywhere. Haven't found any reference on how to use it anywhere either.

A little help?

Thanks!

nicholasthompson’s picture

Are you using it on its own or as part of something like CCK? On its own, you need to read the header of the module. You do something like:
/files/imagecache/[mode]/[width]/[height]/[path to file]

kindafun’s picture

Thanks! I should have mentioned that I'm trying to use it with CCK. I did see the example function call, but with CCK is that necessary?

kindafun’s picture

Oops again! I'm also using the imagefield with all the latest patches.

kindafun’s picture

I'm sorry about this, but maybe it will help someone coming later...

I can see that I can use this function in a CCK template file, is that it's intended use?

nicholasthompson’s picture

I had to tweak a theme template file for my CCK content type (eg, node-content-project.tpl.php... where project is my CCK node type).

From in there I had to something like this:

<?php
  foreach($field_the_screenshots as $screenshot) {
    echo "<a href=\"/imagepreview/".$screenshot['fid']."\"><img src=\"/files/imagecache/scale/192/160/".$screenshot['filepath']."\" /></a>";
  }
?>

Ignore the image preview bit - that was a module I wrote myself which use a menu hook to pickup the imagepreview, then looked up the file by ID and generated a node with a screenshot in it... You'll be interesting in the image source bit.

kindafun’s picture

I appreciate your help here, but I still don't get it. It appears that this would result in something like the following output (exlcluding the anchor tag):

Only local images are allowed.

If I'm suppose to end up with a directory structure like that it's not happening.

dopry’s picture

Title: Tweaked the imagecache_cache() function » create recursive mkdir for imagecache.
Category: feature » bug
Status: Needs review » Fixed

I've applied the patch which provides the recursive mkdir, along with a major rewrite of imagecache.

marcoBauli’s picture

Status: Fixed » Active

When i create my first ruleset and flush it, no directory path is automatically created, and i get the following warning:

    * Flush Ruleset: 1
    * rulesetdir: sites/mysite.com/files/imagecache/thumbnails/sites/mysite.com/files

    * warning: opendir(sites/mysite.com/files/imagecache/thumbnails/sites/mysite.com/files): failed to open dir: No such file or directory in /home/mysite/public_html/drupal/modules/imagecache/imagecache.module on line 357.
    * warning: readdir(): supplied argument is not a valid Directory resource in /home/mysite/public_html/drupal/modules/imagecache/imagecache.module on line 358.

Note: i am running Drupal 4.7.2 in multisite, latest imagecache v. 1.8.

marcoBauli’s picture

aha, thanks Dopry, i got bit better how it works now.

But i tryed inserting different possibilities of code in my Contemplate teaser:

<?php print '<img src="/files/imagecache/thumbnails/'. $node->$field_main_image[0]['filepath'] . '">'; ?>


<?php foreach ((array)$field_main_image as $item) { ?>
      <div class="field-item"><?php print '<img src="/files/imagecache/thumbnails/'.$node->$item[0]['filepath']. '">'; ?></div>
    <?php } ?>


<?php print '<img src="/files/imagecache/thumbnails/'.$field_main_image[0]['filepath']. '">'; ?>

.. still no image appears..

May Contemplate be the cause? or that no directory path was generated on first submission of the ruleset (see the note on the initial post)?

marcoBauli’s picture

ops...sorry, #14 above has been posted on the wrong issue! :P

dopry’s picture

Status: Active » Fixed

the ruleset flush fails because the directory will not exist until a cached image is created. I'm setting back to fixed as the original recursive mkdir issue is closed. please do not open already closed issues for new bugs.

Anonymous’s picture

Status: Fixed » Closed (fixed)