Jump to content

Adding category image, programmatically, issues


Symfony

Recommended Posts

I need to auto add a category image, when the category is created, this is what i do:
 

                        /* Get ext */
                        $size = getimagesize($this->url . str_replace(" ", "%20", $ImagePath));
                        $extension = image_type_to_extension($size[2]);
 
                        /* Get img */
                        $n_image = file_get_contents($this->url . str_replace(" ", "%20", $ImagePath));
 
                        /* Get name */
                        $parts = explode("/", $ImagePath);
 
                        file_put_contents($_SERVER['DOCUMENT_ROOT'] . __PS_BASE_URI__ . "img/c/" . $categoryID . $extension, $n_image);
 
                        $img = new ImageCore();
                        $img->id_product = $cat->id;
                        $img->cover = true;
                        $img->position = 1;
                        $img->image_format = str_replace(".", null, $extension);
                        $img->legend = $parts[count($parts) - 1];
                        $img->add();

How would i add the data? And how do i associate it with the category?I have the ID $cat->id;

Is there a method, where i can provide the image data, or the path to the image, where it will auto create the dimensions required and associate it with the specific catagory->id?
Edited by Symfony (see edit history)
Link to comment
Share on other sites

  • 4 years later...

we prepare code for synchronize with other software. Here you an example

$object->name = array((int)Configuration::get('PS_LANG_DEFAULT') => $nombre);
$object->link_rewrite = array((int)Configuration::get('PS_LANG_DEFAULT') => str2url($nombre));
$object->description =  array((int)Configuration::get('PS_LANG_DEFAULT') => $nombre);;
$object->meta_title = array((int)Configuration::get('PS_LANG_DEFAULT') => $nombre);
$object->meta_description = array((int)Configuration::get('PS_LANG_DEFAULT') => $nombre);
$object->meta_keywords = array ( (int)Configuration::get('PS_LANG_DEFAULT') => getMetaKeywords($nombre,2));

$object->id_parent = $parent;
$object->save();

$image = new Image();
if (!copyImg2($object->id, $image->id, $imagen, 'categories', true))
{
   $image->delete();
}



function copyImg2($id_entity, $id_image, $url, $entity = 'products', $regenerate = true) {
    $tmpfile = tempnam(_PS_TMP_IMG_DIR_, 'ps_import');
    $watermark_types = explode(',', Configuration::get('WATERMARK_TYPES'));
    switch ($entity) {
        default:
        case 'products':
            $image_obj = new Image($id_image);
            $path = $image_obj->getPathForCreation();
            break;
        case 'categories':
            $path = _PS_CAT_IMG_DIR_ . (int) $id_entity;
            break;
        case 'manufacturers':
            $path = _PS_MANU_IMG_DIR_ . (int) $id_entity;
            break;
        case 'suppliers':
            $path = _PS_SUPP_IMG_DIR_ . (int) $id_entity;
            break;
    }
    $url = str_replace(' ', '%20', trim($url));
    if (!ImageManager::checkImageMemoryLimit($url))
        return false;
        if (Tools::copy($url, $tmpfile)) {
            ImageManager::resize($tmpfile, $path . '.jpg');
            $images_types = ImageType::getImagesTypes($entity);
            if ($regenerate)
                foreach ($images_types as $image_type) {
                    ImageManager::resize($tmpfile, $path . '-' . stripslashes($image_type['name']) . '.jpg', $image_type['width'], $image_type['height']);
                    if (in_array($image_type['id_image_type'], $watermark_types))
                        Hook::exec('actionWatermark', array('id_image' => $id_image, 'id_product' => $id_entity));
                }
        }
        else {
            unlink($tmpfile);
            return false;
        }
        unlink($tmpfile);
        return true;
}
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...