Skip to content

Instantly share code, notes, and snippets.

@johnsardine
Created October 19, 2012 11:55
Show Gist options
  • Save johnsardine/3917845 to your computer and use it in GitHub Desktop.
Save johnsardine/3917845 to your computer and use it in GitHub Desktop.
Generate multiple image sizes upon upload
// Load do wideimage
$image = WideImage::load($file_path);
// Processar cada tamanho de imagem
foreach ($this->image_sizes as $key => $image_size) {
if ($image_size['crop'] === true) {
$image->resize($image_size['width'], $image_size['height'], 'outside', 'down')->crop('center', 'top', $image_size['width'], $image_size['height'])->saveToFile($uploads_dir.$file_name_a.'-'.$key.$file_name_b);
} else {
$image->resize($image_size['width'], $image_size['height'], 'inside', 'down')->saveToFile($uploads_dir.$file_name_a.'-'.$key.$file_name_b);
}
}
// Aqui apanhas o ficheiro com o path do sistema, nao url
$image_attributes = getimagesize($file_path);
// Eu tenho um array onde especifico os tamahos de imagem, isto é a adicao do tamanho original mesmo antes de processar tudo
$this->image_sizes['original'] = array(
'width' => $image_attributes[0],
'height' => $image_attributes[1],
'crop' => false
);
// Armazenar os dados de cada tamanho num array
$attachment_meta = array();
// Para cada tamanho de imagem processo os dados do mesmo
foreach ($this->image_sizes as $key => $image_size) {
switch ($key) {
case 'original' :
$attachment_meta[$key] = array(
'src' => $this->config->base_url.'uploads/'.$file_name,
'width' => $image_size['width'],
'height' => $image_size['height']
);
break;
default :
$image_path = $uploads_dir . $file_name_a.'-'.$key.$file_name_b;
$image_attributes = getimagesize($image_path);
$attachment_meta[$key] = array(
'src' => $this->config->base_url.'uploads/'.$file_name_a.'-'.$key.$file_name_b,
'width' => $image_attributes[0],
'height' => $image_attributes[1]
);
break;
}
}
// Save each image size meta data
foreach ($attachment_meta as $key => $value) {
update_item_meta($attachment_id, $key, json_encode($value));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment