public function LocalFolderProvider::generateImage in Devel images provider 7
Same name and namespace in other branches
- 8 plugins/devel_image_provider/provider/LocalFolderProvider.class.php \LocalFolderProvider::generateImage()
Image generation handler.
Overrides DevelImagesProviderBase::generateImage
File
- plugins/
devel_image_provider/ provider/ LocalFolderProvider.class.php, line 49 - Local folder support class.
Class
- LocalFolderProvider
- Add support for local images.
Code
public function generateImage($object, $field, $instance, $bundle) {
$object_field = array();
static $available_images = array();
if (empty($available_images)) {
$available_images = $this
->getImages();
}
if (empty($available_images)) {
$args = func_get_args();
return call_user_func_array('_image_devel_generate', $args);
}
$extension = array_rand(array(
'jpg' => 'jpg',
'png' => 'png',
));
$min_resolution = empty($instance['settings']['min_resolution']) ? '100x100' : $instance['settings']['min_resolution'];
$max_resolution = empty($instance['settings']['max_resolution']) ? '600x600' : $instance['settings']['max_resolution'];
if (FALSE === ($tmp_file = drupal_tempnam('temporary://', 'imagefield_'))) {
return FALSE;
}
$destination = $tmp_file . '.' . $extension;
file_unmanaged_move($tmp_file, $destination, FILE_EXISTS_REPLACE);
$rand_file = array_rand($available_images);
if (!empty($instance['settings']['file_directory'])) {
$instance['settings']['file_directory'] = $instance['settings']['file_directory'] . '/';
}
$destination_dir = $field['settings']['uri_scheme'] . '://' . $instance['settings']['file_directory'];
file_prepare_directory($destination_dir, FILE_CREATE_DIRECTORY);
if ($this->settings['devel_image_no_alter']) {
$file = $available_images[$rand_file];
$file = file_copy($file, $destination_dir);
}
else {
$image = image_load($rand_file);
$min = explode('x', $min_resolution);
$max = explode('x', $max_resolution);
$max[0] = $max[0] < $min[0] ? $min[0] : $max[0];
$max[1] = $max[1] < $min[1] ? $min[1] : $max[1];
$width = rand((int) $min[0], (int) $max[0]);
$height = rand((int) $min[1], (int) $max[1]);
if (!image_scale_and_crop($image, $width, $height)) {
return FALSE;
}
// Use destination image type.
$image->info['extension'] = $extension;
if (!image_save($image, $destination)) {
return FALSE;
}
$source = new stdClass();
$source->uri = $destination;
$source->uid = 1;
// TODO: randomize? Use case specific.
$source->filemime = $image->info['mime_type'];
$source->filename = drupal_basename($image->source);
$destination = $destination_dir . basename($destination);
$file = file_move($source, $destination, FILE_CREATE_DIRECTORY);
}
$object_field['fid'] = $file->fid;
$object_field['alt'] = devel_create_greeking(4);
$object_field['title'] = devel_create_greeking(4);
return $object_field;
}