Genera un'immagine con Drupal imagecache prima di utilizzare imagecache_create_path & getimagesize
-
23-10-2019 - |
Domanda
sto usando imagecache_create_path () e getimagesize () per ottenere il percorso di un'immagine imagecache generate e le sue dimensioni. Tuttavia, se è la prima volta che si accede alla pagina che l'immagine non esiste ancora e imagecache_create_path non genera neanche.
Ecco il codice:
// we get the image path from a preset (always return the path even if the file doesn't exist)
$small_image_path = imagecache_create_path('gallery_image_small', $image["filepath"]);
// I get the image dimensions (only if the file exists already)
$data_small = list($width, $height, $type, $image_attributes) = @getimagesize($small_image_path);
Esiste un metodo API per ottenere il percorso e generare il file? In altre parole, posso generare l'immagine (usando un'impostazione predefinita) da PHP senza mostrare nel browser?
Grazie in anticipo
Soluzione
Controllare il funzionamento imagecache_build_derivative()
e il suo utilizzo in imagecache.module. Per il vostro caso, dovrebbe funzionare più o meno in questo modo:
$presetname = 'gallery_image_small';
$preset = imagecache_preset_by_name($presetname);
$src = $image["filepath"];
$dst = imagecache_create_path($presetname, $src);
// Ensure existing derivative or try to create it on the fly
if (file_exists($dst) || imagecache_build_derivative($preset['actions'], $src, $dst)) {
// Do what you need to do with the image
}
(Nota: codice non testato, guardatevi refusi e altri errori / sviste)